<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1"><meta name="format-detection" content="telephone=no"><meta name="apple-mobile-web-app-capable" content="yes"><meta name="apple-mobile-web-app-status-bar-style" content="black"><link rel="icon" href="/images/icons/favicon-16x16.png?v=2.7.0" type="image/png" sizes="16x16"><link rel="icon" href="/images/icons/favicon-32x32.png?v=2.7.0" type="image/png" sizes="32x32"><meta name="baidu-site-verification" content="code-aMPs2J7ZPy"><meta name="description" content="1. 概述                           1.1 java语言的三个版本        JavaSE: Java 语言的（标准版），用于桌面应用的开发，是其他两个版本的基础  JavaME: Java 语言的（小型版），用于嵌入式消费类电子设备  JavaEE: Java 语言的（企业版），用于 Web 方向的网站开发">
<meta property="og:type" content="article">
<meta property="og:title" content="1.Java基础语法">
<meta property="og:url" content="http://bujiuzhi.gitee.io/2022/02/12/1.Java%E5%9F%BA%E7%A1%80%E8%AF%AD%E6%B3%95/index.html">
<meta property="og:site_name" content="星辰大海">
<meta property="og:description" content="1. 概述                           1.1 java语言的三个版本        JavaSE: Java 语言的（标准版），用于桌面应用的开发，是其他两个版本的基础  JavaME: Java 语言的（小型版），用于嵌入式消费类电子设备  JavaEE: Java 语言的（企业版），用于 Web 方向的网站开发">
<meta property="og:locale" content="en_US">
<meta property="og:image" content="https://s2.loli.net/2022/02/12/7fm82MspKjnFNbA.png">
<meta property="og:image" content="https://s2.loli.net/2022/02/12/s4kZ126ljcCrARx.png">
<meta property="og:image" content="https://s2.loli.net/2022/02/12/neH3aLdwqZEvtWT.png">
<meta property="og:image" content="https://s2.loli.net/2022/02/12/BrsQhcxOFLZ1Kp8.png">
<meta property="og:image" content="https://s2.loli.net/2022/02/12/e2UGxc6ZXRSvphk.png">
<meta property="og:image" content="https://s2.loli.net/2022/02/12/3wyDlNK9GsEjoVx.png">
<meta property="og:image" content="https://s2.loli.net/2022/02/12/nQDogGRCWe7Tqbv.png">
<meta property="og:image" content="https://s2.loli.net/2022/02/12/1HGoZYEKbOgzJic.png">
<meta property="og:image" content="https://s2.loli.net/2022/02/12/8lvFMXC4YUuEIAj.png">
<meta property="og:image" content="https://s2.loli.net/2022/02/12/57opuGVhIjN8ZLM.png">
<meta property="og:image" content="https://s2.loli.net/2022/02/12/rsfln1Me73GES6I.png">
<meta property="og:image" content="https://s2.loli.net/2022/02/12/Y9isNanmwChKfU6.png">
<meta property="og:image" content="https://s2.loli.net/2022/02/12/fUzRokXqeDAgriL.png">
<meta property="og:image" content="https://s2.loli.net/2022/02/12/hYF5depH4jliuAx.png">
<meta property="og:image" content="https://s2.loli.net/2022/02/13/CHU18JMcmglVviQ.png">
<meta property="article:published_time" content="2022-02-12T10:47:43.000Z">
<meta property="article:modified_time" content="2022-02-13T15:21:12.000Z">
<meta property="article:author" content="不久">
<meta property="article:tag" content="学习笔记">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://s2.loli.net/2022/02/12/7fm82MspKjnFNbA.png"><title>1.Java基础语法 | 星辰大海</title><link ref="canonical" href="http://bujiuzhi.gitee.io/2022/02/12/1.Java%E5%9F%BA%E7%A1%80%E8%AF%AD%E6%B3%95/"><link rel="dns-prefetch" href="https://cdn.jsdelivr.net"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.12.1/css/all.min.css" type="text/css"><link rel="stylesheet" href="/css/index.css?v=2.7.0"><link rel="dns-prefetch" href="https://hm.baidu.com"><script>var _hmt = _hmt || [];
(function() {
  var hm = document.createElement('script');
  hm.src = 'https://hm.baidu.com/hm.js?12b4347d0a1f32de969a2276a08cb466';
  hm.async = true;

  if (false) {
    hm.setAttribute('data-pjax', '');
  }
  var s = document.getElementsByTagName('script')[0]; 
  s.parentNode.insertBefore(hm, s);
})();</script><script>var Stun = window.Stun || {};
var CONFIG = {
  root: '/',
  algolia: undefined,
  assistSearch: undefined,
  fontIcon: {"prompt":{"success":"fas fa-check-circle","info":"fas fa-arrow-circle-right","warning":"fas fa-exclamation-circle","error":"fas fa-times-circle"},"copyBtn":"fas fa-copy"},
  sidebar: {"offsetTop":"20px","tocMaxDepth":8},
  header: {"enable":true,"showOnPost":false,"scrollDownIcon":false},
  postWidget: {"endText":true},
  nightMode: {"enable":true},
  back2top: {"enable":true},
  codeblock: {"style":"carbon","highlight":"light","wordWrap":false},
  reward: true,
  fancybox: false,
  zoomImage: {"gapAside":"20px"},
  galleryWaterfall: undefined,
  lazyload: false,
  pjax: undefined,
  externalLink: {"icon":{"enable":true,"name":"fas fa-external-link-alt"}},
  shortcuts: undefined,
  prompt: {"copyButton":"Copy","copySuccess":"Copy Success","copyError":"Copy Error"},
  sourcePath: {"js":"js","css":"css","images":"images"},
};

window.CONFIG = CONFIG;</script><meta name="generator" content="Hexo 6.3.0"></head><body><div class="container" id="container"><header class="header" id="header"><div class="header-inner header-inner--height header-inner--bgcolor"><nav class="header-nav header-nav--sticky"><div class="header-nav-inner"><div class="header-nav-menubtn"><i class="fas fa-bars"></i></div><div class="header-nav-menu"><div class="header-nav-menu-item"><a class="header-nav-menu-item__link" href="/"><span class="header-nav-menu-item__icon"><i class="fas fa-home"></i></span><span class="header-nav-menu-item__text">Home</span></a></div><div class="header-nav-menu-item"><a class="header-nav-menu-item__link" href="/archives/"><span class="header-nav-menu-item__icon"><i class="fas fa-folder-open"></i></span><span class="header-nav-menu-item__text">Archives</span></a></div><div class="header-nav-menu-item"><a class="header-nav-menu-item__link" href="/categories/"><span class="header-nav-menu-item__icon"><i class="fas fa-layer-group"></i></span><span class="header-nav-menu-item__text">Categories</span></a></div><div class="header-nav-menu-item"><a class="header-nav-menu-item__link" href="/tags/"><span class="header-nav-menu-item__icon"><i class="fas fa-tags"></i></span><span class="header-nav-menu-item__text">Tags</span></a></div></div><div class="header-nav-mode"><div class="mode"><div class="mode-track"><span class="mode-track-moon"></span><span class="mode-track-sun"></span></div><div class="mode-thumb"></div></div></div></div></nav></div></header><main class="main" id="main"><div class="main-inner"><div class="content-wrap" id="content-wrap"><div class="content" id="content"><!-- Just used to judge whether it is an article page--><div id="is-post"></div><div class="post"><header class="post-header"><h1 class="post-title">1.Java基础语法</h1><div class="post-meta"><span class="post-meta-item post-meta-item--createtime"><span class="post-meta-item__icon"><i class="far fa-calendar-plus"></i></span><span class="post-meta-item__info">Created</span><span class="post-meta-item__value">2022-02-12</span></span><span class="post-meta-item post-meta-item--updatetime"><span class="post-meta-item__icon"><i class="far fa-calendar-check"></i></span><span class="post-meta-item__info">Updated</span><span class="post-meta-item__value">2022-02-13</span></span></div></header><div class="post-body">
        <h1 id="1-概述"   >
          <a href="#1-概述" class="heading-link"><i class="fas fa-link"></i></a><a href="#1-概述" class="headerlink" title="1. 概述"></a>1. 概述</h1>
      
        <h2 id="1-1-java语言的三个版本"   >
          <a href="#1-1-java语言的三个版本" class="heading-link"><i class="fas fa-link"></i></a><a href="#1-1-java语言的三个版本" class="headerlink" title="1.1 java语言的三个版本"></a>1.1 java语言的三个版本</h2>
      <p> JavaSE: Java 语言的（标准版），用于桌面应用的开发，是其他两个版本的基础</p>
<p> JavaME: Java 语言的（小型版），用于嵌入式消费类电子设备</p>
<p> JavaEE: Java 语言的（企业版），用于 Web 方向的网站开发</p>

        <h2 id="1-2-Java语言跨平台原理"   >
          <a href="#1-2-Java语言跨平台原理" class="heading-link"><i class="fas fa-link"></i></a><a href="#1-2-Java语言跨平台原理" class="headerlink" title="1.2 Java语言跨平台原理"></a>1.2 Java语言跨平台原理</h2>
      <p>Java编译器将Java源程序编译成与平台无关的字节码文件(class文件)，然后由Java虚拟机（JVM）对字节码文件解释执行。</p>
<p><img src="https://s2.loli.net/2022/02/12/7fm82MspKjnFNbA.png" alt="Java跨平台原理"></p>

        <h2 id="1-3-JRE和JDK"   >
          <a href="#1-3-JRE和JDK" class="heading-link"><i class="fas fa-link"></i></a><a href="#1-3-JRE和JDK" class="headerlink" title="1.3 JRE和JDK"></a>1.3 JRE和JDK</h2>
      <p>JVM（Java Virtual Machine），Java虚拟机</p>
<p>JRE（Java Runtime Environment），Java运行环境，包含了JVM和Java的核心类库（Java API）</p>
<p>JDK（Java Development Kit）称为Java开发工具，包含了JRE和开发工具</p>
<p>总结：我们只需安装JDK即可，它包含了java的运行环境和虚拟机。</p>
<p><img src="https://s2.loli.net/2022/02/12/s4kZ126ljcCrARx.png" alt="image-20220212185728089"></p>

        <h3 id="1-3-1-JDK的安装目录介绍"   >
          <a href="#1-3-1-JDK的安装目录介绍" class="heading-link"><i class="fas fa-link"></i></a><a href="#1-3-1-JDK的安装目录介绍" class="headerlink" title="1.3.1 JDK的安装目录介绍"></a>1.3.1 JDK的安装目录介绍</h3>
      <div class="table-container"><table>
<thead>
<tr>
<th>目录名称</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>bin</td>
<td>该路径下存放了JDK的各种工具命令。javac和java就放在这个目录。</td>
</tr>
<tr>
<td>conf</td>
<td>该路径下存放了JDK的相关配置文件。</td>
</tr>
<tr>
<td>include</td>
<td>该路径下存放了一些平台特定的头文件。</td>
</tr>
<tr>
<td>jmods</td>
<td>该路径下存放了JDK的各种模块。</td>
</tr>
<tr>
<td>legal</td>
<td>该路径下存放了JDK各模块的授权文档。</td>
</tr>
<tr>
<td>lib</td>
<td>该路径下存放了JDK工具的一些补充JAR包。</td>
</tr>
</tbody></table></div>

        <h2 id="1-4-HelloWorld案例的编译和运行"   >
          <a href="#1-4-HelloWorld案例的编译和运行" class="heading-link"><i class="fas fa-link"></i></a><a href="#1-4-HelloWorld案例的编译和运行" class="headerlink" title="1.4 HelloWorld案例的编译和运行"></a>1.4 HelloWorld案例的编译和运行</h2>
      <p>打开命令行窗口，将目录切换至java文件所在目录，编译java文件生成class文件，运行class文件。</p>
<blockquote>
<p>编译：javac 文件名.java</p>
<p>范例：javac HelloWorld.java</p>
<p>运行：java 类名</p>
<p>范例：java HelloWorld</p>
</blockquote>

        <h2 id="1-5-关键字"   >
          <a href="#1-5-关键字" class="heading-link"><i class="fas fa-link"></i></a><a href="#1-5-关键字" class="headerlink" title="1.5 关键字"></a>1.5 关键字</h2>
      <p>关键字是指被java语言赋予了特殊含义的单词。</p>
<p>关键字的特点：</p>
<p>​	关键字的字母全部小写。</p>
<p>​	常用的代码编辑器对关键字都有高亮显示，比如现在我们能看到的public、class、static等。</p>

        <h2 id="1-6-常量"   >
          <a href="#1-6-常量" class="heading-link"><i class="fas fa-link"></i></a><a href="#1-6-常量" class="headerlink" title="1.6 常量"></a>1.6 常量</h2>
      <p>常量：在程序运行过程中，其值不可以发生改变的量。</p>
<p>Java中的常量分类：</p>
<blockquote>
<p>字符串常量  用双引号括起来的多个字符（可以包含0个、一个或多个），例如”a”、”abc”、”中国”等</p>
<p>整数常量  整数，例如：-10、0、88等</p>
<p>小数常量  小数，例如：-5.5、1.0、88.88等</p>
<p>字符常量  用单引号括起来的一个字符，例如：’a’、’5’、’B’、’中’等</p>
<p>布尔常量  布尔值，表示真假，只有两个值true和false</p>
<p>空常量  一个特殊的值，空值，值为null</p>
</blockquote>
<p>除空常量外，其他常量均可使用输出语句直接输出。</p>

        <h2 id="1-7-变量"   >
          <a href="#1-7-变量" class="heading-link"><i class="fas fa-link"></i></a><a href="#1-7-变量" class="headerlink" title="1.7 变量"></a>1.7 变量</h2>
      
        <h3 id="1-7-1-变量的定义"   >
          <a href="#1-7-1-变量的定义" class="heading-link"><i class="fas fa-link"></i></a><a href="#1-7-1-变量的定义" class="headerlink" title="1.7.1 变量的定义"></a>1.7.1 变量的定义</h3>
      <p>变量：在程序运行过程中，其值可以发生改变的量。</p>
<p>从本质上讲，变量是内存中的一小块区域，其值可以在一定范围内变化。</p>
<p>变量的定义格式：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">数据类型 变量名 = 初始化值; <span class="comment">// 声明变量并赋值</span></span><br><span class="line"><span class="type">int</span> <span class="variable">age</span> <span class="operator">=</span> <span class="number">18</span>;</span><br><span class="line">System.out.println(age);</span><br></pre></td></tr></table></div></figure>

<p>或者(扩展)</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 先声明，后赋值（使用前赋值即可）</span></span><br><span class="line">数据类型 变量名;</span><br><span class="line">变量名 = 初始化值;</span><br><span class="line"><span class="type">double</span> money;</span><br><span class="line">money = <span class="number">55.5</span>;</span><br><span class="line">System.out.println(money);</span><br></pre></td></tr></table></div></figure>

<p>还可以(扩展)</p>
<p>在同一行定义多个同一种数据类型的变量，中间使用逗号隔开。但不建议使用这种方式，降低程序的可读性。</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">int</span> <span class="variable">a</span> <span class="operator">=</span> <span class="number">10</span>, b = <span class="number">20</span>; <span class="comment">// 定义int类型的变量a和b，中间使用逗号隔开</span></span><br><span class="line">System.out.println(a);</span><br><span class="line">System.out.println(b);</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> c,d; <span class="comment">// 声明int类型的变量c和d，中间使用逗号隔开</span></span><br><span class="line">c = <span class="number">30</span>;</span><br><span class="line">d = <span class="number">40</span>;</span><br><span class="line">System.out.println(c);</span><br><span class="line">System.out.println(d);</span><br></pre></td></tr></table></div></figure>


        <h3 id="1-7-2-变量的修改"   >
          <a href="#1-7-2-变量的修改" class="heading-link"><i class="fas fa-link"></i></a><a href="#1-7-2-变量的修改" class="headerlink" title="1.7.2 变量的修改"></a>1.7.2 变量的修改</h3>
      <figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">int</span> <span class="variable">a</span> <span class="operator">=</span> <span class="number">10</span>;</span><br><span class="line">a = <span class="number">30</span>;  <span class="comment">//修改变量的值</span></span><br><span class="line">System.out.println(a);</span><br></pre></td></tr></table></div></figure>

<p>变量前面不加数据类型时，表示修改已存在的变量的值。</p>

        <h3 id="1-7-3-变量的注意事项"   >
          <a href="#1-7-3-变量的注意事项" class="heading-link"><i class="fas fa-link"></i></a><a href="#1-7-3-变量的注意事项" class="headerlink" title="1.7.3 变量的注意事项"></a>1.7.3 变量的注意事项</h3>
      <ol>
<li>在同一对花括号中，变量名不能重复。</li>
<li>变量在使用之前，必须初始化（赋值）。</li>
<li>定义long类型的变量时，需要在整数的后面加L（大小写均可，建议大写）。因为整数默认是int类型，整数太大可能超出int范围。</li>
<li>定义float类型的变量时，需要在小数的后面加F（大小写均可，建议大写）。因为浮点数的默认类型是double， double的取值范围是大于float的，类型不兼容。</li>
</ol>

        <h2 id="1-8-数据类型"   >
          <a href="#1-8-数据类型" class="heading-link"><i class="fas fa-link"></i></a><a href="#1-8-数据类型" class="headerlink" title="1.8 数据类型"></a>1.8 数据类型</h2>
      <p>Java是一个强类型语言，Java中的数据必须明确数据类型。在Java中的数据类型包括基本数据类型和引用数据类型两种。</p>
<p>Java中的基本数据类型：</p>
<div class="table-container"><table>
<thead>
<tr>
<th align="left">数据类型</th>
<th>关键字</th>
<th>内存占用</th>
<th align="left">取值范围</th>
</tr>
</thead>
<tbody><tr>
<td align="left">整数类型</td>
<td>byte</td>
<td>1</td>
<td align="left">-128~127</td>
</tr>
<tr>
<td align="left"></td>
<td>short</td>
<td>2</td>
<td align="left">-32768~32767</td>
</tr>
<tr>
<td align="left"></td>
<td>int(默认)</td>
<td>4</td>
<td align="left">-2的31次方到2的31次方-1</td>
</tr>
<tr>
<td align="left"></td>
<td>long</td>
<td>8</td>
<td align="left">-2的63次方到2的63次方-1</td>
</tr>
<tr>
<td align="left">浮点类型</td>
<td>float</td>
<td>4</td>
<td align="left">负数：-3.402823E+38到-1.401298E-45                                                             正数：   1.401298E-45到3.402823E+38</td>
</tr>
<tr>
<td align="left"></td>
<td>double(默认)</td>
<td>8</td>
<td align="left">负数：-1.797693E+308到-4.9000000E-324                                              正数：4.9000000E-324   到1.797693E+308</td>
</tr>
<tr>
<td align="left">字符类型</td>
<td>char</td>
<td>2</td>
<td align="left">0-65535</td>
</tr>
<tr>
<td align="left">布尔类型</td>
<td>boolean</td>
<td>1</td>
<td align="left">true，false</td>
</tr>
</tbody></table></div>
<p>说明：</p>
<p>​	e+38表示是乘以10的38次方，同样，e-45表示乘以10的负45次方。</p>
<p>​	在java中整数默认是int类型，浮点数默认是double类型。</p>

        <h2 id="1-9-标识符"   >
          <a href="#1-9-标识符" class="heading-link"><i class="fas fa-link"></i></a><a href="#1-9-标识符" class="headerlink" title="1.9 标识符"></a>1.9 标识符</h2>
      <p>标识符是用户编程时使用的名字，用于给类、方法、变量、常量等命名。</p>

        <h3 id="1-9-1-组成规则："   >
          <a href="#1-9-1-组成规则：" class="heading-link"><i class="fas fa-link"></i></a><a href="#1-9-1-组成规则：" class="headerlink" title="1.9.1 组成规则："></a>1.9.1 组成规则：</h3>
      <p>​	由字母、数字、下划线“_”、美元符号“$”组成，第一个字符不能是数字。</p>
<p>​	不能使用java中的关键字作为标识符。	</p>
<p>​	标识符对大小写敏感（区分大小写）。</p>

        <h3 id="1-9-2-命名约定："   >
          <a href="#1-9-2-命名约定：" class="heading-link"><i class="fas fa-link"></i></a><a href="#1-9-2-命名约定：" class="headerlink" title="1.9.2 命名约定："></a>1.9.2 命名约定：</h3>
      
        <h4 id="1-9-2-1-小驼峰式命名：变量名、方法名"   >
          <a href="#1-9-2-1-小驼峰式命名：变量名、方法名" class="heading-link"><i class="fas fa-link"></i></a><a href="#1-9-2-1-小驼峰式命名：变量名、方法名" class="headerlink" title="1.9.2.1 小驼峰式命名：变量名、方法名"></a>1.9.2.1 小驼峰式命名：变量名、方法名</h4>
      <p>​		首字母小写，从第二个单词开始每个单词的首字母大写。</p>
<p>​		例如：userName</p>

        <h4 id="1-9-2-2-大驼峰式命名：类名"   >
          <a href="#1-9-2-2-大驼峰式命名：类名" class="heading-link"><i class="fas fa-link"></i></a><a href="#1-9-2-2-大驼峰式命名：类名" class="headerlink" title="1.9.2.2 大驼峰式命名：类名"></a>1.9.2.2 大驼峰式命名：类名</h4>
      <p>​		每个单词的首字母都大写。</p>
<p>​		例如：Student</p>

        <h1 id="2-类型转换"   >
          <a href="#2-类型转换" class="heading-link"><i class="fas fa-link"></i></a><a href="#2-类型转换" class="headerlink" title="2.类型转换"></a>2.类型转换</h1>
      <p>在Java中，一些数据类型之间是可以相互转换的。分为两种情况：自动类型转换和强制类型转换。</p>

        <h2 id="2-1-隐式转换（自动类型转换）"   >
          <a href="#2-1-隐式转换（自动类型转换）" class="heading-link"><i class="fas fa-link"></i></a><a href="#2-1-隐式转换（自动类型转换）" class="headerlink" title="2.1 隐式转换（自动类型转换）"></a>2.1 隐式转换（自动类型转换）</h2>
      <p>​	把一个表示数据范围小的数值或者变量赋值给另一个表示数据范围大的变量。这种转换方式是自动的，直接书写即可。例如：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">double</span> <span class="variable">num</span> <span class="operator">=</span> <span class="number">10</span>; <span class="comment">// 将int类型的10直接赋值给double类型</span></span><br><span class="line">System.out.println(num); <span class="comment">// 输出10.0</span></span><br></pre></td></tr></table></div></figure>

<p>​	类型从小到大关系图：</p>
<p><img src="https://s2.loli.net/2022/02/12/neH3aLdwqZEvtWT.png" alt="图片1"></p>
<p>说明：</p>
<ol>
<li>整数默认是int类型，byte、short和char类型数据参与运算均会自动转换为int类型。</li>
</ol>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">byte</span> <span class="variable">b1</span> <span class="operator">=</span> <span class="number">10</span>;</span><br><span class="line"><span class="type">byte</span> <span class="variable">b2</span> <span class="operator">=</span> <span class="number">20</span>;</span><br><span class="line"><span class="type">byte</span> <span class="variable">b3</span> <span class="operator">=</span> b1 + b2; </span><br><span class="line"><span class="comment">// 第三行代码会报错，b1和b2会自动转换为int类型，计算结果为int，int赋值给byte需要强制类型转换。</span></span><br><span class="line"><span class="comment">// 修改为:</span></span><br><span class="line"><span class="type">int</span> <span class="variable">num</span> <span class="operator">=</span> b1 + b2;</span><br><span class="line"><span class="comment">// 或者：</span></span><br><span class="line"><span class="type">byte</span> <span class="variable">b3</span> <span class="operator">=</span> (<span class="type">byte</span>) (b1 + b2);</span><br></pre></td></tr></table></div></figure>

<ol start="2">
<li>boolean类型不能与其他基本数据类型相互转换。</li>
</ol>

        <h2 id="2-2-强制转换"   >
          <a href="#2-2-强制转换" class="heading-link"><i class="fas fa-link"></i></a><a href="#2-2-强制转换" class="headerlink" title="2.2 强制转换"></a>2.2 强制转换</h2>
      <p>​	把一个表示数据范围大的数值或者变量赋值给另一个表示数据范围小的变量。</p>
<p>​	强制类型转换格式：目标数据类型 变量名 &#x3D; (目标数据类型)值或者变量;</p>
<p>​	例如：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">double</span> <span class="variable">num1</span> <span class="operator">=</span> <span class="number">5.5</span>;</span><br><span class="line"><span class="type">int</span> <span class="variable">num2</span> <span class="operator">=</span> (<span class="type">int</span>) num1; <span class="comment">// 将double类型的num1强制转换为int类型</span></span><br><span class="line">System.out.println(num2); <span class="comment">// 输出5（小数位直接舍弃）</span></span><br></pre></td></tr></table></div></figure>


        <h2 id="2-3-类型转换案例"   >
          <a href="#2-3-类型转换案例" class="heading-link"><i class="fas fa-link"></i></a><a href="#2-3-类型转换案例" class="headerlink" title="2.3 类型转换案例"></a>2.3 类型转换案例</h2>
      <p>案例代码：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">byte</span> <span class="variable">a</span> <span class="operator">=</span> <span class="number">3</span>;</span><br><span class="line"><span class="type">byte</span> <span class="variable">b</span> <span class="operator">=</span> <span class="number">4</span>;</span><br><span class="line"><span class="type">byte</span> <span class="variable">c</span> <span class="operator">=</span> a + b; <span class="comment">//错误。因为两个byte变量相加，会先提升为int类型</span></span><br><span class="line"><span class="type">byte</span> <span class="variable">d</span> <span class="operator">=</span> <span class="number">3</span> + <span class="number">4</span>; <span class="comment">//正确。常量优化机制</span></span><br></pre></td></tr></table></div></figure>

<p>常量优化机制：</p>
<p>​	在编译时，整数常量的计算会直接算出结果，并且会自动判断该结果是否在byte取值范围内，</p>
<blockquote>
<p>在：编译通过<br>        不在：编译失败</p>
</blockquote>

        <h1 id="3-运算符"   >
          <a href="#3-运算符" class="heading-link"><i class="fas fa-link"></i></a><a href="#3-运算符" class="headerlink" title="3.运算符"></a>3.运算符</h1>
      
        <h2 id="3-1-算数运算符"   >
          <a href="#3-1-算数运算符" class="heading-link"><i class="fas fa-link"></i></a><a href="#3-1-算数运算符" class="headerlink" title="3.1 算数运算符"></a>3.1 算数运算符</h2>
      <div class="table-container"><table>
<thead>
<tr>
<th>符号</th>
<th>作用</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>+</td>
<td>加</td>
<td>和值</td>
</tr>
<tr>
<td>-</td>
<td>减</td>
<td>差值</td>
</tr>
<tr>
<td>*</td>
<td>乘</td>
<td>与“×”相同</td>
</tr>
<tr>
<td>&#x2F;</td>
<td>除</td>
<td>与“÷”相同</td>
</tr>
<tr>
<td>%</td>
<td>取余</td>
<td>获取的是两个数据做除法的余数</td>
</tr>
</tbody></table></div>
<p>注意：</p>
<ol>
<li><p>&#x2F;和%的区别：两个数据做除法，&#x2F;取结果的商，%取结果的余数。</p>
</li>
<li><p>整数操作只能得到整数，要想得到小数，必须有浮点数参与运算。</p>
</li>
</ol>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">int</span> <span class="variable">a</span> <span class="operator">=</span> <span class="number">10</span>;</span><br><span class="line"><span class="type">int</span> <span class="variable">b</span> <span class="operator">=</span> <span class="number">3</span>;</span><br><span class="line">System.out.println(a / b); <span class="comment">// 输出结果3</span></span><br><span class="line">System.out.println(a % b); <span class="comment">// 输出结果1</span></span><br></pre></td></tr></table></div></figure>


        <h3 id="3-1-1-字符的“-”操作"   >
          <a href="#3-1-1-字符的“-”操作" class="heading-link"><i class="fas fa-link"></i></a><a href="#3-1-1-字符的“-”操作" class="headerlink" title="3.1.1 字符的“+”操作"></a>3.1.1 字符的“+”操作</h3>
      <p>char类型参与算术运算，使用的是计算机底层对应的十进制数值。需要我们记住三个字符对应的数值：</p>
<blockquote>
<p>‘a’  –  97		a-z是连续的，所以’b’对应的数值是98，’c’是99，依次递加</p>
<p>‘A’  –  65		A-Z是连续的，所以’B’对应的数值是66，’C’是67，依次递加</p>
<p>‘0’  –  48		0-9是连续的，所以’1’对应的数值是49，’2’是50，依次递加</p>
</blockquote>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 可以通过使用字符与整数做算术运算，得出字符对应的数值是多少</span></span><br><span class="line"><span class="type">char</span> <span class="variable">ch1</span> <span class="operator">=</span> <span class="string">&#x27;a&#x27;</span>;</span><br><span class="line">System.out.println(ch1 + <span class="number">1</span>); <span class="comment">// 输出98，97 + 1 = 98</span></span><br><span class="line"></span><br><span class="line"><span class="type">char</span> <span class="variable">ch2</span> <span class="operator">=</span> <span class="string">&#x27;A&#x27;</span>;</span><br><span class="line">System.out.println(ch2 + <span class="number">1</span>); <span class="comment">// 输出66，65 + 1 = 66</span></span><br><span class="line"></span><br><span class="line"><span class="type">char</span> <span class="variable">ch3</span> <span class="operator">=</span> <span class="string">&#x27;0&#x27;</span>;</span><br><span class="line">System.out.println(ch3 + <span class="number">1</span>); <span class="comment">// 输出49，48 + 1 = 49</span></span><br></pre></td></tr></table></div></figure>

<p>算术表达式中包含不同的基本数据类型的值的时候，整个算术表达式的类型会自动进行提升。</p>
<p>提升规则：</p>
<p>byte类型，short类型和char类型将被提升到int类型，不管是否有其他类型参与运算。</p>
<p>整个表达式的类型自动提升到与表达式中最高等级的操作数相同的类型</p>
<p>​       等级顺序：byte,short,char –&gt; int –&gt; long –&gt; float –&gt; double</p>
<p>例如：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">byte</span> <span class="variable">b1</span> <span class="operator">=</span> <span class="number">10</span>;</span><br><span class="line"><span class="type">byte</span> <span class="variable">b2</span> <span class="operator">=</span> <span class="number">20</span>;</span><br><span class="line"><span class="comment">// byte b3 = b1 + b2; // 该行报错，因为byte类型参与算术运算会自动提示为int，int赋值给byte可能损失精度</span></span><br><span class="line"><span class="type">int</span> <span class="variable">i3</span> <span class="operator">=</span> b1 + b2; <span class="comment">// 应该使用int接收</span></span><br><span class="line"><span class="type">byte</span> <span class="variable">b3</span> <span class="operator">=</span> (<span class="type">byte</span>) (b1 + b2); <span class="comment">// 或者将结果强制转换为byte类型</span></span><br><span class="line">-------------------------------</span><br><span class="line"><span class="type">int</span> <span class="variable">num1</span> <span class="operator">=</span> <span class="number">10</span>;</span><br><span class="line"><span class="type">double</span> <span class="variable">num2</span> <span class="operator">=</span> <span class="number">20.0</span>;</span><br><span class="line"><span class="type">double</span> <span class="variable">num3</span> <span class="operator">=</span> num1 + num2; <span class="comment">// 使用double接收，因为num1会自动提升为double类型</span></span><br></pre></td></tr></table></div></figure>


        <h3 id="3-1-2-字符串的“-”操作"   >
          <a href="#3-1-2-字符串的“-”操作" class="heading-link"><i class="fas fa-link"></i></a><a href="#3-1-2-字符串的“-”操作" class="headerlink" title="3.1.2 字符串的“+”操作"></a>3.1.2 字符串的“+”操作</h3>
      <p>当“+”操作中出现字符串时，这个”+”是字符串连接符，而不是算术运算。</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">System.out.println(<span class="string">&quot;itheima&quot;</span>+ <span class="number">666</span>); <span class="comment">// 输出：itheima666</span></span><br></pre></td></tr></table></div></figure>

<p>在”+”操作中，如果出现了字符串，就是连接运算符，否则就是算术运算。当连续进行“+”操作时，从左到右逐个执行。</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">System.out.println(<span class="number">1</span> + <span class="number">99</span> + <span class="string">&quot;年黑马&quot;</span>);            <span class="comment">// 输出：100年黑马</span></span><br><span class="line">System.out.println(<span class="number">1</span> + <span class="number">2</span> + <span class="string">&quot;itheima&quot;</span> + <span class="number">3</span> + <span class="number">4</span>);   <span class="comment">// 输出：3itheima34</span></span><br><span class="line"><span class="comment">// 可以使用小括号改变运算的优先级 </span></span><br><span class="line">System.out.println(<span class="number">1</span> + <span class="number">2</span> + <span class="string">&quot;itheima&quot;</span> + (<span class="number">3</span> + <span class="number">4</span>)); <span class="comment">// 输出：3itheima7</span></span><br></pre></td></tr></table></div></figure>


        <h2 id="3-2-自增自减运算符"   >
          <a href="#3-2-自增自减运算符" class="heading-link"><i class="fas fa-link"></i></a><a href="#3-2-自增自减运算符" class="headerlink" title="3.2 自增自减运算符"></a>3.2 自增自减运算符</h2>
      <div class="table-container"><table>
<thead>
<tr>
<th>符号</th>
<th>作用</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>++</td>
<td>自增</td>
<td>变量的值加1</td>
</tr>
<tr>
<td>–</td>
<td>自减</td>
<td>变量的值减1</td>
</tr>
</tbody></table></div>
<p>注意事项：</p>
<p>​	++和– 既可以放在变量的后边，也可以放在变量的前边。</p>
<p>​	单独使用的时候， ++和– 无论是放在变量的前边还是后边，结果是一样的。</p>
<p>​	放在后面：先运算，后自加（减）。</p>
<p>​	放在前面：先自加（减），后运算。</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">int</span> <span class="variable">i</span> <span class="operator">=</span> <span class="number">10</span>;</span><br><span class="line">i++; <span class="comment">// 单独使用</span></span><br><span class="line">System.out.println(<span class="string">&quot;i:&quot;</span> + i); <span class="comment">// i:11</span></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="variable">j</span> <span class="operator">=</span> <span class="number">10</span>;</span><br><span class="line">++j; <span class="comment">// 单独使用</span></span><br><span class="line">System.out.println(<span class="string">&quot;j:&quot;</span> + j); <span class="comment">// j:11</span></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="variable">x</span> <span class="operator">=</span> <span class="number">10</span>;</span><br><span class="line"><span class="type">int</span> <span class="variable">y</span> <span class="operator">=</span> x++; <span class="comment">// 赋值运算，++在后边，所以是使用x原来的值赋值给y，x本身自增1</span></span><br><span class="line">System.out.println(<span class="string">&quot;x:&quot;</span> + x + <span class="string">&quot;, y:&quot;</span> + y); <span class="comment">// x:11，y:10</span></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="variable">m</span> <span class="operator">=</span> <span class="number">10</span>;</span><br><span class="line"><span class="type">int</span> <span class="variable">n</span> <span class="operator">=</span> ++m; <span class="comment">// 赋值运算，++在前边，所以是使用m自增后的值赋值给n，m本身自增1</span></span><br><span class="line">System.out.println(<span class="string">&quot;m:&quot;</span> + m + <span class="string">&quot;, m:&quot;</span> + m); <span class="comment">// m:11，m:11</span></span><br></pre></td></tr></table></div></figure>


        <h2 id="3-3-赋值运算符"   >
          <a href="#3-3-赋值运算符" class="heading-link"><i class="fas fa-link"></i></a><a href="#3-3-赋值运算符" class="headerlink" title="3.3 赋值运算符"></a>3.3 赋值运算符</h2>
      <p>赋值运算符的作用是将一个表达式的值赋给左边，左边必须是可修改的，不能是常量。</p>
<div class="table-container"><table>
<thead>
<tr>
<th>符号</th>
<th>作用</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>&#x3D;</td>
<td>赋值</td>
<td>a&#x3D;10，将10赋值给变量a</td>
</tr>
<tr>
<td>+&#x3D;</td>
<td>加后赋值</td>
<td>a+&#x3D;b，将a+b的值给a</td>
</tr>
<tr>
<td>-&#x3D;</td>
<td>减后赋值</td>
<td>a-&#x3D;b，将a-b的值给a</td>
</tr>
<tr>
<td>*&#x3D;</td>
<td>乘后赋值</td>
<td>a*&#x3D;b，将a×b的值给a</td>
</tr>
<tr>
<td>&#x2F;&#x3D;</td>
<td>除后赋值</td>
<td>a&#x2F;&#x3D;b，将a÷b的商给a</td>
</tr>
<tr>
<td>%&#x3D;</td>
<td>取余后赋值</td>
<td>a%&#x3D;b，将a÷b的余数给a</td>
</tr>
</tbody></table></div>
<p>注意：</p>
<p>扩展的赋值运算符&#x3D;&#x3D;隐含了强制类型转换&#x3D;&#x3D;。</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">short</span> <span class="variable">s</span> <span class="operator">=</span> <span class="number">10</span>;</span><br><span class="line">s = s + <span class="number">10</span>; <span class="comment">// 此行代码报出，因为运算中s提升为int类型，运算结果int赋值给short可能损失精度</span></span><br><span class="line"></span><br><span class="line">s += <span class="number">10</span>; <span class="comment">// 此行代码没有问题，隐含了强制类型转换，相当于 s = (short) (s + 10);</span></span><br></pre></td></tr></table></div></figure>


        <h2 id="3-4-关系运算符"   >
          <a href="#3-4-关系运算符" class="heading-link"><i class="fas fa-link"></i></a><a href="#3-4-关系运算符" class="headerlink" title="3.4 关系运算符"></a>3.4 关系运算符</h2>
      <p>关系运算符有6种关系，分别为小于、小于等于、大于、等于、大于等于、不等于。</p>
<div class="table-container"><table>
<thead>
<tr>
<th>符号</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>&#x3D;&#x3D;</td>
<td>a&#x3D;&#x3D;b，判断a和b的值是否相等，成立为true，不成立为false</td>
</tr>
<tr>
<td>!&#x3D;</td>
<td>a!&#x3D;b，判断a和b的值是否不相等，成立为true，不成立为false</td>
</tr>
<tr>
<td>&gt;</td>
<td>a&gt;b，判断a是否大于b，成立为true，不成立为false</td>
</tr>
<tr>
<td>&gt;&#x3D;</td>
<td>a&gt;&#x3D;b，判断a是否大于等于b，成立为true，不成立为false</td>
</tr>
<tr>
<td>&lt;</td>
<td>a&lt;b，判断a是否小于b，成立为true，不成立为false</td>
</tr>
<tr>
<td>&lt;&#x3D;</td>
<td>a&lt;&#x3D;b，判断a是否小于等于b，成立为true，不成立为false</td>
</tr>
</tbody></table></div>

        <h2 id="3-5-逻辑运算符"   >
          <a href="#3-5-逻辑运算符" class="heading-link"><i class="fas fa-link"></i></a><a href="#3-5-逻辑运算符" class="headerlink" title="3.5 逻辑运算符"></a>3.5 逻辑运算符</h2>
      <p>逻辑运算符把各个运算的关系表达式连接起来组成一个复杂的逻辑表达式，以判断程序中的表达式是否成立，判断的结果是 true 或 false。</p>
<div class="table-container"><table>
<thead>
<tr>
<th>符号</th>
<th>作用</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>&amp;</td>
<td>逻辑与</td>
<td>a&amp;b，a和b都是true，结果为true，否则为false</td>
</tr>
<tr>
<td>|</td>
<td>逻辑或</td>
<td>a|b，a和b都是false，结果为false，否则为true</td>
</tr>
<tr>
<td>^</td>
<td>逻辑异或</td>
<td>a^b，a和b结果不同为true，相同为false</td>
</tr>
<tr>
<td>!</td>
<td>逻辑非</td>
<td>!a，结果和a的结果正好相反</td>
</tr>
</tbody></table></div>

        <h2 id="3-6-短路逻辑运算符"   >
          <a href="#3-6-短路逻辑运算符" class="heading-link"><i class="fas fa-link"></i></a><a href="#3-6-短路逻辑运算符" class="headerlink" title="3.6 短路逻辑运算符"></a>3.6 短路逻辑运算符</h2>
      <div class="table-container"><table>
<thead>
<tr>
<th>符号</th>
<th>作用</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>&amp;&amp;</td>
<td>短路与</td>
<td>作用和&amp;相同，但是有短路效果</td>
</tr>
<tr>
<td>||</td>
<td>短路或</td>
<td>作用和|相同，但是有短路效果</td>
</tr>
</tbody></table></div>
<p>在逻辑与运算中，只要有一个表达式的值为false，那么结果就可以判定为false了，没有必要将所有表达式的值都计算出来，短路与操作就有这样的效果，可以提高效率。同理在逻辑或运算中，一旦发现值为true，右边的表达式将不再参与运算。</p>
<ul>
<li><p>逻辑与&amp;，无论左边真假，右边都要执行。</p>
</li>
<li><p>短路与&amp;&amp;，如果左边为真，右边执行；如果左边为假，右边不执行。</p>
</li>
<li><p>逻辑或|，无论左边真假，右边都要执行。</p>
</li>
<li><p>短路或||，如果左边为假，右边执行；如果左边为真，右边不执行。</p>
</li>
</ul>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">int</span> <span class="variable">x</span> <span class="operator">=</span> <span class="number">3</span>;</span><br><span class="line"><span class="type">int</span> <span class="variable">y</span> <span class="operator">=</span> <span class="number">4</span>;</span><br><span class="line">System.out.println((x++ &gt; <span class="number">4</span>) &amp; (y++ &gt; <span class="number">5</span>)); <span class="comment">// 两个表达都会运算</span></span><br><span class="line">System.out.println(x); <span class="comment">// 4</span></span><br><span class="line">System.out.println(y); <span class="comment">// 5</span></span><br><span class="line"></span><br><span class="line">System.out.println((x++ &gt; <span class="number">4</span>) &amp;&amp; (y++ &gt; <span class="number">5</span>)); <span class="comment">// 左边已经可以确定结果为false，右边不参与运算</span></span><br><span class="line">System.out.println(x); <span class="comment">// 4</span></span><br><span class="line">System.out.println(y); <span class="comment">// 4</span></span><br></pre></td></tr></table></div></figure>


        <h2 id="3-7-三元运算符"   >
          <a href="#3-7-三元运算符" class="heading-link"><i class="fas fa-link"></i></a><a href="#3-7-三元运算符" class="headerlink" title="3.7 三元运算符"></a>3.7 三元运算符</h2>
      <p>三元运算符语法格式：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">关系表达式 ? 表达式<span class="number">1</span> : 表达式<span class="number">2</span>;</span><br></pre></td></tr></table></div></figure>

<p>解释：问号前面的位置是判断的条件，判断结果为boolean型，为true时调用表达式1，为false时调用表达式2。其逻辑为：如果条件表达式成立或者满足则执行表达式1，否则执行第二个。</p>
<p>举例：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">int</span> <span class="variable">a</span> <span class="operator">=</span> <span class="number">10</span>;</span><br><span class="line"><span class="type">int</span> <span class="variable">b</span> <span class="operator">=</span> <span class="number">20</span>;</span><br><span class="line"><span class="type">int</span> <span class="variable">c</span> <span class="operator">=</span> a &gt; b ? a : b; <span class="comment">// 判断 a&gt;b 是否为真，如果为真取a的值，如果为假，取b的值</span></span><br></pre></td></tr></table></div></figure>




        <h1 id="4-流程控制语句"   >
          <a href="#4-流程控制语句" class="heading-link"><i class="fas fa-link"></i></a><a href="#4-流程控制语句" class="headerlink" title="4. 流程控制语句"></a>4. 流程控制语句</h1>
      
        <h2 id="4-1-顺序结构"   >
          <a href="#4-1-顺序结构" class="heading-link"><i class="fas fa-link"></i></a><a href="#4-1-顺序结构" class="headerlink" title="4.1 顺序结构"></a>4.1 顺序结构</h2>
      <p>顺序结构是程序中最简单最基本的流程控制，没有特定的语法结构，按照代码的先后顺序，依次执行，程序中大多数的代码都是这样执行的。</p>
<p>顺序结构执行流程图：</p>
<p><img src="https://s2.loli.net/2022/02/12/BrsQhcxOFLZ1Kp8.png" alt="image-20220212205710165"></p>

        <h2 id="4-2-分支结构"   >
          <a href="#4-2-分支结构" class="heading-link"><i class="fas fa-link"></i></a><a href="#4-2-分支结构" class="headerlink" title="4.2 分支结构"></a>4.2 分支结构</h2>
      
        <h3 id="4-2-1-if语句格式1"   >
          <a href="#4-2-1-if语句格式1" class="heading-link"><i class="fas fa-link"></i></a><a href="#4-2-1-if语句格式1" class="headerlink" title="4.2.1  if语句格式1"></a>4.2.1  if语句格式1</h3>
      <figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">格式：</span><br><span class="line"><span class="keyword">if</span> (关系表达式) &#123;</span><br><span class="line">    语句体;	</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>

<p>执行流程：</p>
<p>①首先计算关系表达式的值</p>
<p>②如果关系表达式的值为true就执行语句体</p>
<p>③如果关系表达式的值为false就不执行语句体</p>
<p>④继续执行后面的语句内容</p>
<p><img src="https://s2.loli.net/2022/02/12/e2UGxc6ZXRSvphk.png" alt="image-20220212205853564"></p>

        <h3 id="4-2-2-if语句格式2"   >
          <a href="#4-2-2-if语句格式2" class="heading-link"><i class="fas fa-link"></i></a><a href="#4-2-2-if语句格式2" class="headerlink" title="4.2.2 if语句格式2"></a>4.2.2 if语句格式2</h3>
      <figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">格式：</span><br><span class="line"><span class="keyword">if</span> (关系表达式) &#123;</span><br><span class="line">    语句体<span class="number">1</span>;	</span><br><span class="line">&#125; <span class="keyword">else</span> &#123;</span><br><span class="line">    语句体<span class="number">2</span>;	</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>

<p>执行流程：</p>
<p>①首先计算关系表达式的值</p>
<p>②如果关系表达式的值为true就执行语句体1</p>
<p>③如果关系表达式的值为false就执行语句体2</p>
<p>④继续执行后面的语句内容</p>
<p><img src="https://s2.loli.net/2022/02/12/3wyDlNK9GsEjoVx.png" alt="image-20220212210008323"></p>

        <h3 id="4-2-3-if语句格式3"   >
          <a href="#4-2-3-if语句格式3" class="heading-link"><i class="fas fa-link"></i></a><a href="#4-2-3-if语句格式3" class="headerlink" title="4.2.3 if语句格式3"></a>4.2.3 if语句格式3</h3>
      <figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">格式：</span><br><span class="line"><span class="keyword">if</span> (关系表达式<span class="number">1</span>) &#123;</span><br><span class="line">    语句体<span class="number">1</span>;	</span><br><span class="line">&#125; <span class="keyword">else</span> <span class="keyword">if</span> (关系表达式<span class="number">2</span>) &#123;</span><br><span class="line">    语句体<span class="number">2</span>;	</span><br><span class="line">&#125; </span><br><span class="line">…</span><br><span class="line"><span class="keyword">else</span> &#123;</span><br><span class="line">    语句体n+<span class="number">1</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>

<p>执行流程：</p>
<p>①首先计算关系表达式1的值</p>
<p>②如果值为true就执行语句体1；如果值为false就计算关系表达式2的值</p>
<p>③如果值为true就执行语句体2；如果值为false就计算关系表达式3的值</p>
<p>④…</p>
<p>⑤如果没有任何关系表达式为true，就执行语句体n+1。</p>
<p><img src="https://s2.loli.net/2022/02/12/nQDogGRCWe7Tqbv.png" alt="图片5"></p>

        <h2 id="4-2-循环结构"   >
          <a href="#4-2-循环结构" class="heading-link"><i class="fas fa-link"></i></a><a href="#4-2-循环结构" class="headerlink" title="4.2 循环结构"></a>4.2 循环结构</h2>
      
        <h3 id="4-2-1-switch语句"   >
          <a href="#4-2-1-switch语句" class="heading-link"><i class="fas fa-link"></i></a><a href="#4-2-1-switch语句" class="headerlink" title="4.2.1 switch语句"></a>4.2.1 switch语句</h3>
      <ul>
<li><p>格式</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">switch</span> (表达式) &#123;</span><br><span class="line">	<span class="keyword">case</span> <span class="number">1</span>:</span><br><span class="line">		语句体<span class="number">1</span>;</span><br><span class="line">		<span class="keyword">break</span>;</span><br><span class="line">	<span class="keyword">case</span> <span class="number">2</span>:</span><br><span class="line">		语句体<span class="number">2</span>;</span><br><span class="line">		<span class="keyword">break</span>;</span><br><span class="line">	...</span><br><span class="line">	<span class="keyword">default</span>:</span><br><span class="line">		语句体n+<span class="number">1</span>;</span><br><span class="line">		<span class="keyword">break</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>
</li>
<li><p>执行流程：</p>
<ul>
<li>首先计算出表达式的值 </li>
<li>其次，和case依次比较，一旦有对应的值，就会执行相应的语句，在执行的过程中，遇到break就会结束。 </li>
<li>最后，如果所有的case都和表达式的值不匹配，就会执行default语句体部分，然后程序结束掉。</li>
</ul>
</li>
</ul>
<p>注意： 如果switch语句中,case省略了break语句, 就会开始case穿透</p>
<ul>
<li>需求 : 键盘录入星期数，输出工作日、休息日 (1-5)工作日，(6-7)休息日</li>
<li>示例代码：</li>
</ul>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">case穿透是如何产生的?</span></span><br><span class="line"><span class="comment">		</span></span><br><span class="line"><span class="comment">		如果switch语句中,case省略了break语句, 就会开始case穿透.</span></span><br><span class="line"><span class="comment">		</span></span><br><span class="line"><span class="comment">		现象：</span></span><br><span class="line"><span class="comment">			当开始case穿透，后续的case就不会具有匹配效果，内部的语句都会执行</span></span><br><span class="line"><span class="comment">			直到看见break，或者将整体switch语句执行完毕，才会结束。</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span>&#123;</span><br><span class="line">		<span class="type">Scanner</span> <span class="variable">sc</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Scanner</span>(System.in);</span><br><span class="line">		System.out.println(<span class="string">&quot;请输入星期数:&quot;</span>);</span><br><span class="line">		<span class="type">int</span> <span class="variable">week</span> <span class="operator">=</span> sc.nextInt();</span><br><span class="line">		</span><br><span class="line">		<span class="keyword">switch</span>(week)&#123;</span><br><span class="line">			<span class="keyword">case</span> <span class="number">1</span>:</span><br><span class="line">			<span class="keyword">case</span> <span class="number">2</span>:</span><br><span class="line">			<span class="keyword">case</span> <span class="number">3</span>:</span><br><span class="line">			<span class="keyword">case</span> <span class="number">4</span>:</span><br><span class="line">			<span class="keyword">case</span> <span class="number">5</span>:</span><br><span class="line">				System.out.println(<span class="string">&quot;工作日&quot;</span>);</span><br><span class="line">				<span class="keyword">break</span>;</span><br><span class="line">			<span class="keyword">case</span> <span class="number">6</span>:</span><br><span class="line">			<span class="keyword">case</span> <span class="number">7</span>:</span><br><span class="line">				System.out.println(<span class="string">&quot;休息日&quot;</span>);</span><br><span class="line">				<span class="keyword">break</span>;</span><br><span class="line">			<span class="keyword">default</span>:</span><br><span class="line">				System.out.println(<span class="string">&quot;您的输入有误&quot;</span>);</span><br><span class="line">				<span class="keyword">break</span>;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;	</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h3 id="4-2-2-for循环"   >
          <a href="#4-2-2-for循环" class="heading-link"><i class="fas fa-link"></i></a><a href="#4-2-2-for循环" class="headerlink" title="4.2.2 for循环"></a>4.2.2 for循环</h3>
      <ul>
<li><p>循环：</p>
<p>循环语句可以在满足循环条件的情况下，反复执行某一段代码，这段被重复执行的代码被称为循环体语句，当反复 执行这个循环体时，需要在合适的时候把循环判断条件修改为false，从而结束循环，否则循环将一直执行下去，形 成死循环。 </p>
</li>
<li><p>for循环格式：</p>
</li>
</ul>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">for</span> (初始化语句;条件判断语句;条件控制语句) &#123;</span><br><span class="line">	循环体语句;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>

<ul>
<li><p>格式解释：</p>
<ul>
<li>初始化语句：  用于表示循环开启时的起始状态，简单说就是循环开始的时候什么样</li>
<li>条件判断语句：用于表示循环反复执行的条件，简单说就是判断循环是否能一直执行下去</li>
<li>循环体语句：  用于表示循环反复执行的内容，简单说就是循环反复执行的事情</li>
<li>条件控制语句：用于表示循环执行中每次变化的内容，简单说就是控制循环是否能执行下去</li>
</ul>
</li>
<li><p>执行流程：</p>
<p>①执行初始化语句</p>
<p>②执行条件判断语句，看其结果是true还是false</p>
<p>​             如果是false，循环结束</p>
<p>​             如果是true，继续执行</p>
<p>③执行循环体语句</p>
<p>④执行条件控制语句</p>
<p>⑤回到②继续</p>
</li>
</ul>

        <h3 id="4-2-3-while循环"   >
          <a href="#4-2-3-while循环" class="heading-link"><i class="fas fa-link"></i></a><a href="#4-2-3-while循环" class="headerlink" title="4.2.3 while循环"></a>4.2.3 while循环</h3>
      <ul>
<li><p>while循环完整格式：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">初始化语句;</span><br><span class="line"><span class="keyword">while</span> (条件判断语句) &#123;</span><br><span class="line">	循环体语句;</span><br><span class="line">    条件控制语句;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>
</li>
<li><p>while循环执行流程：</p>
<p>①执行初始化语句</p>
<p>②执行条件判断语句，看其结果是true还是false</p>
<p>​             如果是false，循环结束</p>
<p>​             如果是true，继续执行</p>
<p>③执行循环体语句</p>
<p>④执行条件控制语句</p>
<p>⑤回到②继续</p>
</li>
</ul>

        <h3 id="4-2-4-do……while循环"   >
          <a href="#4-2-4-do……while循环" class="heading-link"><i class="fas fa-link"></i></a><a href="#4-2-4-do……while循环" class="headerlink" title="4.2.4 do……while循环"></a>4.2.4 do……while循环</h3>
      <ul>
<li><p>完整格式：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">初始化语句;</span><br><span class="line"><span class="keyword">do</span> &#123;</span><br><span class="line">	循环体语句;</span><br><span class="line">	条件控制语句;</span><br><span class="line">&#125;<span class="keyword">while</span>(条件判断语句);</span><br></pre></td></tr></table></div></figure>
</li>
<li><p>执行流程：</p>
<p>① 执行初始化语句</p>
<p>② 执行循环体语句</p>
<p>③ 执行条件控制语句</p>
<p>④ 执行条件判断语句，看其结果是true还是false</p>
<p>如果是false，循环结束</p>
<p>如果是true，继续执行</p>
<p>⑤ 回到②继续</p>
</li>
</ul>

        <h3 id="4-2-5-三种循环的区别"   >
          <a href="#4-2-5-三种循环的区别" class="heading-link"><i class="fas fa-link"></i></a><a href="#4-2-5-三种循环的区别" class="headerlink" title="4.2.5 三种循环的区别"></a>4.2.5 三种循环的区别</h3>
      <ul>
<li>三种循环的区别<ul>
<li>for循环和while循环先判断条件是否成立，然后决定是否执行循环体（先判断后执行）</li>
<li>do…while循环先执行一次循环体，然后判断条件是否成立，是否继续执行循环体（先执行后判断）</li>
</ul>
</li>
<li>for循环和while的区别<ul>
<li>条件控制语句所控制的自增变量，因为归属for循环的语法结构中，在for循环结束后，就不能再次被访问到了</li>
<li>条件控制语句所控制的自增变量，对于while循环来说不归属其语法结构中，在while循环结束后，该变量还可以继续使用</li>
</ul>
</li>
</ul>

        <h3 id="4-2-6-死循环"   >
          <a href="#4-2-6-死循环" class="heading-link"><i class="fas fa-link"></i></a><a href="#4-2-6-死循环" class="headerlink" title="4.2.6 死循环"></a>4.2.6 死循环</h3>
      <ul>
<li><p>死循环格式		</p>
<figure class="highlight plaintext"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">for死循环格式 :</span><br><span class="line">for(;;)&#123;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">while死循环格式 :</span><br><span class="line"></span><br><span class="line">while(true)&#123;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">do..while死循环格式 :</span><br><span class="line"></span><br><span class="line">do&#123;</span><br><span class="line"></span><br><span class="line">&#125;while(true);</span><br></pre></td></tr></table></div></figure></li>
</ul>

        <h3 id="4-2-7-跳转控制语句"   >
          <a href="#4-2-7-跳转控制语句" class="heading-link"><i class="fas fa-link"></i></a><a href="#4-2-7-跳转控制语句" class="headerlink" title="4.2.7 跳转控制语句"></a>4.2.7 跳转控制语句</h3>
      <ul>
<li>跳转控制语句（break）<ul>
<li>跳出循环，结束循环</li>
</ul>
</li>
<li>跳转控制语句（continue）<ul>
<li>跳过本次循环，继续下次循环</li>
</ul>
</li>
<li>注意： continue只能在循环中进行使用！</li>
</ul>

        <h1 id="5-Random"   >
          <a href="#5-Random" class="heading-link"><i class="fas fa-link"></i></a><a href="#5-Random" class="headerlink" title="5.Random"></a>5.Random</h1>
      
        <h2 id="5-1-Random产生随机数（掌握）"   >
          <a href="#5-1-Random产生随机数（掌握）" class="heading-link"><i class="fas fa-link"></i></a><a href="#5-1-Random产生随机数（掌握）" class="headerlink" title="5.1 Random产生随机数（掌握）"></a>5.1 Random产生随机数（掌握）</h2>
      <ul>
<li><p>概述：</p>
<ul>
<li>Random类似Scanner，也是Java提供好的API，内部提供了产生随机数的功能</li>
</ul>
</li>
<li><p>使用步骤：</p>
<ol>
<li><p>导入包</p>
<p><code>import java.util.Random;</code></p>
</li>
<li><p>创建对象</p>
<p><code>Random r = new Random();</code></p>
</li>
<li><p>产生随机数</p>
<p><code>int num = r.nextInt(10);</code></p>
<p>解释：产生的随机数是0-9中随机的一个</p>
</li>
</ol>
</li>
<li><p>示例代码：</p>
</li>
</ul>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> java.util.Random;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Demo1Random</span> &#123;</span><br><span class="line">	<span class="comment">/*</span></span><br><span class="line"><span class="comment">		Random : 产生随机数</span></span><br><span class="line"><span class="comment">		</span></span><br><span class="line"><span class="comment">		1. 导包	: import java.util.Random;</span></span><br><span class="line"><span class="comment">				    导包的动作必须出现在类定义的上面</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">		2. 创建对象 : Random r = new Random();</span></span><br><span class="line"><span class="comment">					上面这个格式里面，r 是变量名，可以变，其他的都不允许变</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">		3. 获取随机数 : int number = r.nextInt(10);	//获取数据的范围：[0,10) 包括0,不包括10</span></span><br><span class="line"><span class="comment">					上面这个格式里面，number是变量名，可以变，数字10可以变。其他的都不允许变</span></span><br><span class="line"><span class="comment">		</span></span><br><span class="line"><span class="comment">		需求: 产生随机数1-10之间的</span></span><br><span class="line"><span class="comment">	*/</span></span><br><span class="line">	<span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span>&#123;</span><br><span class="line">		<span class="comment">// 2. 创建对象</span></span><br><span class="line">		<span class="type">Random</span> <span class="variable">r</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Random</span>();</span><br><span class="line">		</span><br><span class="line">		<span class="keyword">for</span>(<span class="type">int</span> <span class="variable">i</span> <span class="operator">=</span> <span class="number">1</span>; i &lt;= <span class="number">10</span>; i++)&#123;</span><br><span class="line">			<span class="comment">// 3. 获取随机数</span></span><br><span class="line">			<span class="type">int</span> <span class="variable">num</span> <span class="operator">=</span> r.nextInt(<span class="number">10</span>) + <span class="number">1</span>;		<span class="comment">// 1-10</span></span><br><span class="line">			System.out.println(num);</span><br><span class="line">		&#125;	</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h1 id="6-数组"   >
          <a href="#6-数组" class="heading-link"><i class="fas fa-link"></i></a><a href="#6-数组" class="headerlink" title="6.数组"></a>6.数组</h1>
      
        <h2 id="6-1-数组介绍"   >
          <a href="#6-1-数组介绍" class="heading-link"><i class="fas fa-link"></i></a><a href="#6-1-数组介绍" class="headerlink" title="6.1 数组介绍"></a>6.1 数组介绍</h2>
      <p>​	数组就是存储数据长度固定的容器，存储多个数据的数据类型要一致。 </p>

        <h2 id="6-2-数组的定义格式"   >
          <a href="#6-2-数组的定义格式" class="heading-link"><i class="fas fa-link"></i></a><a href="#6-2-数组的定义格式" class="headerlink" title="6.2 数组的定义格式"></a>6.2 数组的定义格式</h2>
      <ul>
<li>第一种格式</li>
</ul>
<p>​	数据类型[] 数组名</p>
<p>​	示例：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">int</span>[] arr;        </span><br><span class="line"><span class="type">double</span>[] arr;      </span><br><span class="line"><span class="type">char</span>[] arr;</span><br></pre></td></tr></table></div></figure>

<ul>
<li>第二种格式</li>
</ul>
<p>​	数据类型 数组名[]</p>
<p>​	示例：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">int</span> arr[];</span><br><span class="line"><span class="type">double</span> arr[];</span><br><span class="line"><span class="type">char</span> arr[];</span><br></pre></td></tr></table></div></figure>


        <h2 id="6-3-动态初始化"   >
          <a href="#6-3-动态初始化" class="heading-link"><i class="fas fa-link"></i></a><a href="#6-3-动态初始化" class="headerlink" title="6.3 动态初始化"></a>6.3 动态初始化</h2>
      <p>数组动态初始化就是只给定数组的长度，由系统给出默认初始化值</p>

        <h3 id="6-3-1-动态初始化格式"   >
          <a href="#6-3-1-动态初始化格式" class="heading-link"><i class="fas fa-link"></i></a><a href="#6-3-1-动态初始化格式" class="headerlink" title="6.3.1 动态初始化格式"></a>6.3.1 动态初始化格式</h3>
      <figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">数据类型[] 数组名 = <span class="keyword">new</span> 数据类型[数组长度];</span><br></pre></td></tr></table></div></figure>

<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">int</span>[] arr = <span class="keyword">new</span> <span class="title class_">int</span>[<span class="number">3</span>];</span><br></pre></td></tr></table></div></figure>


        <h3 id="6-3-2-示例代码"   >
          <a href="#6-3-2-示例代码" class="heading-link"><i class="fas fa-link"></i></a><a href="#6-3-2-示例代码" class="headerlink" title="6.3.2 示例代码 :"></a>6.3.2 示例代码 :</h3>
      <figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.itheima.array;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Demo2Array</span> &#123;</span><br><span class="line">    <span class="comment">/*</span></span><br><span class="line"><span class="comment">        数组的动态初始化:</span></span><br><span class="line"><span class="comment">                        在初始化的时候, 需要手动指定数组的长度, 系统会为数组容器分配初始值.</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">        动态初始化格式:</span></span><br><span class="line"><span class="comment">                        数据类型[] 数组名 = new 数据类型[数组的长度];</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">        注意:</span></span><br><span class="line"><span class="comment">                        打印数组变量的时候, 会打印出数组的内存地址</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">        [I@10f87f48 :</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">                        @ : 分隔符</span></span><br><span class="line"><span class="comment">                        [ : 当前的空间是一个数组类型</span></span><br><span class="line"><span class="comment">                        I : 当前数组容器中所存储的数据类型</span></span><br><span class="line"><span class="comment">                        10f87f48 : 十六进制内存地址</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">                                0 1 2 3 4 5 6 7 8 9 a b c d e f</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="comment">// 数据类型[] 数组名 = new 数据类型[数组的长度];</span></span><br><span class="line">        <span class="comment">// 通过new关键字创建了一个int类型的数组容器, 该容器可以存储5个int类型的整数, 该容器被arr数组变量所记录</span></span><br><span class="line">        <span class="type">int</span>[] arr = <span class="keyword">new</span> <span class="title class_">int</span>[<span class="number">5</span>];</span><br><span class="line">        <span class="comment">// [I@10f87f48</span></span><br><span class="line">        System.out.println(arr);</span><br><span class="line"></span><br><span class="line">        <span class="type">byte</span>[] bArr = <span class="keyword">new</span> <span class="title class_">byte</span>[<span class="number">3</span>];</span><br><span class="line">        <span class="comment">// [B@b4c966a</span></span><br><span class="line">        System.out.println(bArr);</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></div></figure>


        <h2 id="6-4-数组的静态初始化"   >
          <a href="#6-4-数组的静态初始化" class="heading-link"><i class="fas fa-link"></i></a><a href="#6-4-数组的静态初始化" class="headerlink" title="6.4 数组的静态初始化"></a>6.4 数组的静态初始化</h2>
      <p>​	在创建数组时，直接将元素确定	</p>

        <h3 id="6-4-1-静态初始化格式"   >
          <a href="#6-4-1-静态初始化格式" class="heading-link"><i class="fas fa-link"></i></a><a href="#6-4-1-静态初始化格式" class="headerlink" title="6.4.1 静态初始化格式"></a>6.4.1 静态初始化格式</h3>
      <ul>
<li><p>完整版格式</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">数据类型[] 数组名 = <span class="keyword">new</span> 数据类型[]&#123;元素<span class="number">1</span>,元素<span class="number">2</span>,...&#125;;</span><br></pre></td></tr></table></div></figure>
</li>
<li><p>简化版格式</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">数据类型[] 数组名 = &#123;元素<span class="number">1</span>,元素<span class="number">2</span>,...&#125;;</span><br></pre></td></tr></table></div></figure></li>
</ul>

        <h3 id="6-4-2-示例代码"   >
          <a href="#6-4-2-示例代码" class="heading-link"><i class="fas fa-link"></i></a><a href="#6-4-2-示例代码" class="headerlink" title="6.4.2 示例代码"></a>6.4.2 示例代码</h3>
      <figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.itheima.array2;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Demo1Array</span> &#123;</span><br><span class="line">    <span class="comment">/*</span></span><br><span class="line"><span class="comment">        数组静态初始化 : 初始化时指定每个数组元素的初始值，由系统决定数组长度</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">        完整格式:</span></span><br><span class="line"><span class="comment">                    数据类型[] 数组名 = new 数据类型[]&#123;数据1,数据2,数据3...&#125;;</span></span><br><span class="line"><span class="comment">        简化格式:</span></span><br><span class="line"><span class="comment">                    数据类型[] 数组名 = &#123;数据1,数据2,数据3...&#125;;</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="comment">// 数据类型[] 数组名 = new 数据类型[]&#123;数据1,数据2,数据3...&#125;;</span></span><br><span class="line">        <span class="type">int</span>[] arr = <span class="keyword">new</span> <span class="title class_">int</span>[]&#123;<span class="number">11</span>,<span class="number">22</span>,<span class="number">33</span>&#125;;</span><br><span class="line">        System.out.println(arr[<span class="number">0</span>]);</span><br><span class="line">        System.out.println(arr[<span class="number">1</span>]);</span><br><span class="line">        System.out.println(arr[<span class="number">2</span>]);</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 数据类型[] 数组名 = &#123;数据1,数据2,数据3...&#125;;</span></span><br><span class="line">        <span class="type">int</span>[] arr2 = &#123;<span class="number">44</span>,<span class="number">55</span>,<span class="number">66</span>&#125;;</span><br><span class="line">        System.out.println(arr2);</span><br><span class="line">        System.out.println(arr2[<span class="number">0</span>]);</span><br><span class="line">        System.out.println(arr2[<span class="number">1</span>]);</span><br><span class="line">        System.out.println(arr2[<span class="number">2</span>]);</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h2 id="6-5-内存分配"   >
          <a href="#6-5-内存分配" class="heading-link"><i class="fas fa-link"></i></a><a href="#6-5-内存分配" class="headerlink" title="6.5 内存分配"></a>6.5 内存分配</h2>
      <ul>
<li>一个数组内存图</li>
</ul>
<p><img src="https://s2.loli.net/2022/02/12/1HGoZYEKbOgzJic.png" alt="1591007817165"></p>
<ul>
<li>两个数组内存图</li>
</ul>
<p><img src="https://s2.loli.net/2022/02/12/8lvFMXC4YUuEIAj.png" alt="1591007925899"></p>
<ul>
<li>多个数组指向相同内存图</li>
</ul>
<p><img src="https://s2.loli.net/2022/02/12/57opuGVhIjN8ZLM.png" alt="1591007957052"></p>

        <h1 id="7-方法"   >
          <a href="#7-方法" class="heading-link"><i class="fas fa-link"></i></a><a href="#7-方法" class="headerlink" title="7.方法"></a>7.方法</h1>
      
        <h2 id="7-1-无参数方法定义和调用"   >
          <a href="#7-1-无参数方法定义和调用" class="heading-link"><i class="fas fa-link"></i></a><a href="#7-1-无参数方法定义和调用" class="headerlink" title="7.1 无参数方法定义和调用"></a>7.1 无参数方法定义和调用</h2>
      <ul>
<li><p>定义格式：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> 方法名 (   ) &#123;</span><br><span class="line">	<span class="comment">// 方法体;</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>
</li>
<li><p>范例：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">method</span> <span class="params">(    )</span> &#123;</span><br><span class="line">	<span class="comment">// 方法体;</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>
</li>
<li><p>调用格式：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">方法名();</span><br></pre></td></tr></table></div></figure>
</li>
<li><p>范例：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">method();</span><br></pre></td></tr></table></div></figure>
</li>
<li><p>注意：</p>
<ul>
<li>方法必须先定义，后调用，否则程序将报错</li>
<li>每个方法在被调用执行的时候，都会进入栈内存，并且拥有自己独立的内存空间，方法内部代码调用完毕之后，会从栈内存中弹栈消失。</li>
</ul>
</li>
</ul>

        <h2 id="7-2-带参数方法定义和调用"   >
          <a href="#7-2-带参数方法定义和调用" class="heading-link"><i class="fas fa-link"></i></a><a href="#7-2-带参数方法定义和调用" class="headerlink" title="7.2 带参数方法定义和调用"></a>7.2 带参数方法定义和调用</h2>
      <ul>
<li><p>定义格式：</p>
<p>参数：由数据类型和变量名组成 -  数据类型 变量名</p>
<p>参数范例：int a</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> 方法名 (参数<span class="number">1</span>) &#123;</span><br><span class="line">	方法体;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> 方法名 (参数<span class="number">1</span>, 参数<span class="number">2</span>, 参数<span class="number">3.</span>..) &#123;</span><br><span class="line">	方法体;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>
</li>
<li><p>范例：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">isEvenNumber</span><span class="params">(<span class="type">int</span> number)</span>&#123;</span><br><span class="line">    ...</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">getMax</span><span class="params">(<span class="type">int</span> num1, <span class="type">int</span> num2)</span>&#123;</span><br><span class="line">    ...</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>
</li>
<li><p>注意：</p>
<figure class="highlight plaintext"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">方法定义时，参数中的数据类型与变量名都不能缺少，缺少任意一个程序将报错</span><br><span class="line"></span><br><span class="line">方法定义时，多个参数之间使用逗号( ，)分隔</span><br></pre></td></tr></table></div></figure>
</li>
<li><p>调用格式：</p>
<figure class="highlight plaintext"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">方法名(参数)；</span><br><span class="line"></span><br><span class="line">方法名(参数1,参数2);</span><br></pre></td></tr></table></div></figure>
</li>
<li><p>范例：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">isEvenNumber(<span class="number">10</span>);</span><br><span class="line"></span><br><span class="line">getMax(<span class="number">10</span>,<span class="number">20</span>);</span><br></pre></td></tr></table></div></figure>

<p>方法调用时，参数的数量与类型必须与方法定义中的设置相匹配，否则程序将报错</p>
</li>
</ul>
<p><strong>形参：方法定义中的参数</strong></p>
<p>​          等同于变量定义格式，例如：int number</p>
<p><strong>实参：方法调用中的参数</strong></p>
<p>​          等同于使用变量或常量，例如： 10  number</p>

        <h2 id="7-3-带返回值方法定义和调用"   >
          <a href="#7-3-带返回值方法定义和调用" class="heading-link"><i class="fas fa-link"></i></a><a href="#7-3-带返回值方法定义和调用" class="headerlink" title="7.3 带返回值方法定义和调用"></a>7.3 带返回值方法定义和调用</h2>
      <ul>
<li><p>定义格式</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> 数据类型 方法名 ( 参数 ) &#123; </span><br><span class="line">	<span class="keyword">return</span> 数据 ;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>
</li>
<li><p>范例</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> <span class="type">boolean</span> <span class="title function_">isEvenNumber</span><span class="params">( <span class="type">int</span> number )</span> &#123;           </span><br><span class="line">	<span class="keyword">return</span> <span class="literal">true</span> ;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> <span class="type">int</span> <span class="title function_">getMax</span><span class="params">( <span class="type">int</span> a, <span class="type">int</span> b )</span> &#123;</span><br><span class="line">	<span class="keyword">return</span>  <span class="number">100</span> ;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>
</li>
<li><p>注意：</p>
<p>​	方法定义时return后面的返回值与方法定义上的数据类型要匹配，否则程序将报错</p>
</li>
<li><p>调用格式</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">方法名 ( 参数 ) ;</span><br><span class="line">数据类型 变量名 = 方法名 ( 参数 ) ;</span><br></pre></td></tr></table></div></figure>
</li>
<li><p>范例</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">isEvenNumber ( <span class="number">5</span> ) ;</span><br><span class="line"><span class="type">boolean</span>  <span class="variable">flag</span> <span class="operator">=</span>  isEvenNumber ( <span class="number">5</span> ); </span><br></pre></td></tr></table></div></figure></li>
</ul>
<p>方法的返回值通常会使用变量接收，否则该返回值将无意义</p>

        <h2 id="7-4-方法的注意事项"   >
          <a href="#7-4-方法的注意事项" class="heading-link"><i class="fas fa-link"></i></a><a href="#7-4-方法的注意事项" class="headerlink" title="7.4 方法的注意事项"></a>7.4 方法的注意事项</h2>
      <ul>
<li><p>方法不能嵌套定义</p>
<ul>
<li><p>示例代码：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">MethodDemo</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">methodOne</span><span class="params">()</span> &#123;</span><br><span class="line">		<span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">methodTwo</span><span class="params">()</span> &#123;</span><br><span class="line">       		<span class="comment">// 这里会引发编译错误!!!</span></span><br><span class="line">    	&#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure></li>
</ul>
</li>
<li><p>void表示无返回值，可以省略return，也可以单独的书写return，后面不加数据</p>
<ul>
<li><p>示例代码：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">MethodDemo</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">methodTwo</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="comment">//return 100; 编译错误，因为没有具体返回值类型</span></span><br><span class="line">        <span class="keyword">return</span>;	</span><br><span class="line">        <span class="comment">//System.out.println(100); return语句后面不能跟数据或代码</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure></li>
</ul>
</li>
</ul>

        <h2 id="7-5-方法重载-同方法，不同参"   >
          <a href="#7-5-方法重载-同方法，不同参" class="heading-link"><i class="fas fa-link"></i></a><a href="#7-5-方法重载-同方法，不同参" class="headerlink" title="7.5 方法重载(同方法，不同参)"></a>7.5 方法重载(同方法，不同参)</h2>
      <ul>
<li><p>方法重载概念</p>
<p>方法重载指同一个类中定义的多个方法之间的关系，满足下列条件的多个方法相互构成重载</p>
<ul>
<li>多个方法在同一个类中</li>
<li>多个方法具有&#x3D;&#x3D;相同的方法名&#x3D;&#x3D;</li>
<li>多个方法的&#x3D;&#x3D;参数不相同&#x3D;&#x3D;，类型不同或者数量不同</li>
</ul>
</li>
<li><p>注意：</p>
<ul>
<li>重载仅对应方法的定义，与方法的调用无关，调用方式参照标准格式</li>
<li>重载仅针对同一个类中方法的名称与参数进行识别，与返回值无关，换句话说不能通过返回值来判定两个方法是否相互构成重载</li>
</ul>
</li>
<li><p>正确范例：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">MethodDemo</span> &#123;</span><br><span class="line">	<span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">fn</span><span class="params">(<span class="type">int</span> a)</span> &#123;</span><br><span class="line">    	<span class="comment">//方法体</span></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="type">int</span> <span class="title function_">fn</span><span class="params">(<span class="type">double</span> a)</span> &#123;</span><br><span class="line">    	<span class="comment">//方法体</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">MethodDemo</span> &#123;</span><br><span class="line">	<span class="keyword">public</span> <span class="keyword">static</span> <span class="type">float</span> <span class="title function_">fn</span><span class="params">(<span class="type">int</span> a)</span> &#123;</span><br><span class="line">    	<span class="comment">//方法体</span></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="type">int</span> <span class="title function_">fn</span><span class="params">(<span class="type">int</span> a , <span class="type">int</span> b)</span> &#123;</span><br><span class="line">    	<span class="comment">//方法体</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>
</li>
<li><p>错误范例：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">MethodDemo</span> &#123;</span><br><span class="line">	<span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">fn</span><span class="params">(<span class="type">int</span> a)</span> &#123;</span><br><span class="line">    	<span class="comment">//方法体</span></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="type">int</span> <span class="title function_">fn</span><span class="params">(<span class="type">int</span> a)</span> &#123; 	<span class="comment">/*错误原因：重载与返回值无关*/</span></span><br><span class="line">    	<span class="comment">//方法体</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">MethodDemo01</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">fn</span><span class="params">(<span class="type">int</span> a)</span> &#123;</span><br><span class="line">        <span class="comment">//方法体</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125; </span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">MethodDemo02</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="type">int</span> <span class="title function_">fn</span><span class="params">(<span class="type">double</span> a)</span> &#123; <span class="comment">/*错误原因：这是两个类的两个fn方法*/</span></span><br><span class="line">        <span class="comment">//方法体</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure></li>
</ul>

        <h2 id="7-6-方法的参数传递"   >
          <a href="#7-6-方法的参数传递" class="heading-link"><i class="fas fa-link"></i></a><a href="#7-6-方法的参数传递" class="headerlink" title="7.6 方法的参数传递"></a>7.6 方法的参数传递</h2>
      
        <h3 id="7-6-1-传递基本类型"   >
          <a href="#7-6-1-传递基本类型" class="heading-link"><i class="fas fa-link"></i></a><a href="#7-6-1-传递基本类型" class="headerlink" title="7.6.1  传递基本类型"></a>7.6.1  传递基本类型</h3>
      <ul>
<li><p>测试代码：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.itheima.param;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Test1</span> &#123;</span><br><span class="line">    <span class="comment">/*</span></span><br><span class="line"><span class="comment">         方法参数传递为基本数据类型 :</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">                传入方法中的, 是具体的数值.</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="type">int</span> <span class="variable">number</span> <span class="operator">=</span> <span class="number">100</span>;</span><br><span class="line">        System.out.println(<span class="string">&quot;调用change方法前:&quot;</span> + number);</span><br><span class="line">        change(number);</span><br><span class="line">        System.out.println(<span class="string">&quot;调用change方法后:&quot;</span> + number);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">change</span><span class="params">(<span class="type">int</span> number)</span> &#123;</span><br><span class="line">        number = <span class="number">200</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></div></figure>
</li>
<li><p>结论：</p>
<ul>
<li><strong>基本数据类型的参数，形式参数的改变，不影响实际参数</strong></li>
</ul>
</li>
<li><p>结论依据：</p>
<ul>
<li>每个方法在栈内存中，都会有独立的栈空间，方法运行结束后就会弹栈消失</li>
</ul>
</li>
</ul>

        <h3 id="7-6-2-传递引用类型"   >
          <a href="#7-6-2-传递引用类型" class="heading-link"><i class="fas fa-link"></i></a><a href="#7-6-2-传递引用类型" class="headerlink" title="7.6.2 传递引用类型"></a>7.6.2 传递引用类型</h3>
      <ul>
<li><p>测试代码：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.itheima.param;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Test2</span> &#123;</span><br><span class="line">    <span class="comment">/*</span></span><br><span class="line"><span class="comment">         方法参数传递为引用数据类型 :</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">                传入方法中的, 是内存地址.</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="type">int</span>[] arr = &#123;<span class="number">10</span>, <span class="number">20</span>, <span class="number">30</span>&#125;;</span><br><span class="line">        System.out.println(<span class="string">&quot;调用change方法前:&quot;</span> + arr[<span class="number">1</span>]);</span><br><span class="line">        change(arr);</span><br><span class="line">        System.out.println(<span class="string">&quot;调用change方法后:&quot;</span> + arr[<span class="number">1</span>]);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">change</span><span class="params">(<span class="type">int</span>[] arr)</span> &#123;</span><br><span class="line">        arr[<span class="number">1</span>] = <span class="number">200</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>
</li>
<li><p>结论：</p>
<ul>
<li><strong>引用类型的参数，形式参数的改变，影响实际参数的值</strong></li>
</ul>
</li>
<li><p>结论依据：</p>
<ul>
<li>引用数据类型的传参，传入的是地址值，内存中会造成两个引用指向同一个内存的效果，所以即使方法弹栈，堆内存中的数据也已经是改变后的结果</li>
</ul>
</li>
</ul>

        <h1 id="8-进制"   >
          <a href="#8-进制" class="heading-link"><i class="fas fa-link"></i></a><a href="#8-进制" class="headerlink" title="8.进制"></a>8.进制</h1>
      <pre><code>    十进制：Java中，数值默认都是10进制，不需要加任何修饰。
    二进制：数值前面以0b开头，b大小写都可以。
    八进制：数值前面以0开头。
    十六进制：数值前面以0x开头，x大小写都可以。

    注意: 书写的时候, 虽然加入了进制的标识, 但打印在控制台展示的都是十进制数据。
</code></pre>

        <h2 id="8-1-十进制到任意进制的转换"   >
          <a href="#8-1-十进制到任意进制的转换" class="heading-link"><i class="fas fa-link"></i></a><a href="#8-1-十进制到任意进制的转换" class="headerlink" title="8.1 十进制到任意进制的转换"></a>8.1 十进制到任意进制的转换</h2>
      <ul>
<li>十进制——&gt;二进制</li>
</ul>
<p><img src="https://s2.loli.net/2022/02/12/rsfln1Me73GES6I.png" alt="1590936221838"></p>
<ul>
<li>十进制——&gt;十六进制</li>
</ul>
<p><img src="https://s2.loli.net/2022/02/12/Y9isNanmwChKfU6.png" alt="1590936342865"></p>
<p>结论：十进制到任意进制的转换</p>
<p>公式：除基取余使用源数据，不断的除以基数（几进制，基数就是几）得到余数，直到商为0，再将余数倒着	拼起来即可</p>

        <h2 id="8-2-原码、补码、反码"   >
          <a href="#8-2-原码、补码、反码" class="heading-link"><i class="fas fa-link"></i></a><a href="#8-2-原码、补码、反码" class="headerlink" title="8.2 原码、补码、反码"></a>8.2 原码、补码、反码</h2>
      <p>**原码 **:就是二进制定点表示法，即最高位为符号位，【0】表示正，【1】表示负，其余位表示数值的大小。</p>
<p><img src="https://s2.loli.net/2022/02/12/fUzRokXqeDAgriL.png" alt="1590936726746"></p>
<p>若符号为为0，则原、反、补一样；</p>
<p>若符号为为1，则，符号为不变，最后面的1以及后面的位保持不变，其余为取反；</p>

        <h2 id="8-3-位运算"   >
          <a href="#8-3-位运算" class="heading-link"><i class="fas fa-link"></i></a><a href="#8-3-位运算" class="headerlink" title="8.3 位运算"></a>8.3 位运算</h2>
      <p>先将十进制数转成二进制后再进行运算。在二进制位运算中，1表示true，0表示false。</p>

        <h3 id="8-3-1-位运算符介绍"   >
          <a href="#8-3-1-位运算符介绍" class="heading-link"><i class="fas fa-link"></i></a><a href="#8-3-1-位运算符介绍" class="headerlink" title="8.3.1 位运算符介绍 :"></a>8.3.1 位运算符介绍 :</h3>
      <p><img src="https://s2.loli.net/2022/02/12/hYF5depH4jliuAx.png" alt="1590937235620"></p>

        <h3 id="8-3-2-示例代码"   >
          <a href="#8-3-2-示例代码" class="heading-link"><i class="fas fa-link"></i></a><a href="#8-3-2-示例代码" class="headerlink" title="8.3.2 示例代码"></a>8.3.2 示例代码</h3>
      <figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.itheima.demo;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Demo2</span> &#123;</span><br><span class="line">    <span class="comment">/*</span></span><br><span class="line"><span class="comment">        位运算:</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">            位运算符指的是二进制位的运算，先将十进制数转成二进制后再进行运算。</span></span><br><span class="line"><span class="comment">            在二进制位运算中，1表示true，0表示false。</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">             &amp; 位与 : 遇false则false, 遇0则0</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">                        00000000 00000000 00000000 00000110     // 6的二进制</span></span><br><span class="line"><span class="comment">                     &amp;  00000000 00000000 00000000 00000010     // 2的二进制</span></span><br><span class="line"><span class="comment">                    -----------------------------------------</span></span><br><span class="line"><span class="comment">                        00000000 00000000 00000000 00000010     // 结果: 2</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">             | 位或 : 遇true则true, 遇1则1</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">             ^ 位异或 : 相同为false, 不同为true</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">             ~ 取反 : 全部取反, 0变1, 1变0  (也包括符号位)</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">                    00000000 00000000 00000000 00000110         // 6的二进制补码</span></span><br><span class="line"><span class="comment">                  ~ 11111111 11111111 11111111 11111001</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">                  -                                   1         // -1求反码</span></span><br><span class="line"><span class="comment">                   ------------------------------------</span></span><br><span class="line"><span class="comment">                    11111111 11111111 11111111 11111000         // 反码推原码</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">                    10000000 00000000 00000000 00000111         // -7</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        System.out.println(<span class="number">6</span> &amp; <span class="number">2</span>);</span><br><span class="line">        System.out.println(~<span class="number">6</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>

<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.itheima.demo;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Demo3</span> &#123;</span><br><span class="line">    <span class="comment">/*</span></span><br><span class="line"><span class="comment">       位移运算符:</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">               &lt;&lt; 有符号左移运算，二进制位向左移动, 左边符号位丢弃, 右边补齐0</span></span><br><span class="line"><span class="comment">                        运算规律: 向左移动几位, 就是乘以2的几次幂</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">                                12 &lt;&lt; 2</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">                                (0)0000000 00000000 00000000 000011000  // 12的二进制</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">       -----------------------------------------------------------------------------</span></span><br><span class="line"><span class="comment">               &gt;&gt; 有符号右移运算，二进制位向右移动, 使用符号位进行补位</span></span><br><span class="line"><span class="comment">                        运算规律: 向右移动几位, 就是除以2的几次幂</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">                                000000000 00000000 00000000 0000001(1)  // 3的二进制</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">       -----------------------------------------------------------------------------</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">                &gt;&gt;&gt; 无符号右移运算符,  无论符号位是0还是1，都补0</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">                                010000000 00000000 00000000 00000110  // -6的二进制</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        System.out.println(<span class="number">12</span> &lt;&lt; <span class="number">1</span>);  <span class="comment">// 24</span></span><br><span class="line">        System.out.println(<span class="number">12</span> &lt;&lt; <span class="number">2</span>);  <span class="comment">// 48</span></span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></div></figure>

<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.itheima.demo;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Demo4</span> &#123;</span><br><span class="line">    <span class="comment">/*</span></span><br><span class="line"><span class="comment">        ^ 运算符的特点</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">                一个数, 被另外一个数, 异或两次, 该数本身不变</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        System.out.println(<span class="number">10</span> ^ <span class="number">5</span> ^ <span class="number">10</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></div></figure>


        <h1 id="9-二维数组概述"   >
          <a href="#9-二维数组概述" class="heading-link"><i class="fas fa-link"></i></a><a href="#9-二维数组概述" class="headerlink" title="9.二维数组概述"></a>9.二维数组概述</h1>
      <p>​	<strong>概述 :</strong>  二维数组也是一种容器，不同于一维数组，该容器存储的都是一维数组容器</p>

        <h2 id="9-1-二维数组动态初始化"   >
          <a href="#9-1-二维数组动态初始化" class="heading-link"><i class="fas fa-link"></i></a><a href="#9-1-二维数组动态初始化" class="headerlink" title="9.1 二维数组动态初始化"></a>9.1 二维数组动态初始化</h2>
      <figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">动态初始化格式：</span><br><span class="line">数据类型[][] 变量名 = <span class="keyword">new</span> 数据类型[m][n];</span><br><span class="line">m表示这个二维数组，可以存放多少个一维数组</span><br><span class="line">n表示每一个一维数组，可以存放多少个元素</span><br></pre></td></tr></table></div></figure>

<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.itheima.demo;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Demo1Array</span> &#123;</span><br><span class="line">    <span class="comment">/*</span></span><br><span class="line"><span class="comment">        动态初始化格式：</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">            数据类型[][] 变量名 = new 数据类型[m][n];</span></span><br><span class="line"><span class="comment">            m表示这个二维数组，可以存放多少个一维数组</span></span><br><span class="line"><span class="comment">            n表示每一个一维数组，可以存放多少个元素</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="comment">// 数据类型[][] 变量名 = new 数据类型[m][n];</span></span><br><span class="line">        <span class="type">int</span>[][] arr = <span class="keyword">new</span> <span class="title class_">int</span>[<span class="number">3</span>][<span class="number">3</span>];</span><br><span class="line">        <span class="comment">/*</span></span><br><span class="line"><span class="comment">            [[I@10f87f48</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">            @ : 分隔符</span></span><br><span class="line"><span class="comment">            10f87f48 : 十六进制内存地址</span></span><br><span class="line"><span class="comment">            I : 数组中存储的数据类型</span></span><br><span class="line"><span class="comment">            [[ : 几个中括号就代表的是几维数组</span></span><br><span class="line"><span class="comment">         */</span></span><br><span class="line">        System.out.println(arr);</span><br><span class="line"></span><br><span class="line">        <span class="comment">/*</span></span><br><span class="line"><span class="comment">            二维数组存储一维数组的时候, 存储的是一维数组的内存地址</span></span><br><span class="line"><span class="comment">         */</span></span><br><span class="line">        System.out.println(arr[<span class="number">0</span>]);</span><br><span class="line">        System.out.println(arr[<span class="number">1</span>]);</span><br><span class="line">        System.out.println(arr[<span class="number">2</span>]);</span><br><span class="line"></span><br><span class="line">        System.out.println(arr[<span class="number">0</span>][<span class="number">0</span>]);</span><br><span class="line">        System.out.println(arr[<span class="number">1</span>][<span class="number">1</span>]);</span><br><span class="line">        System.out.println(arr[<span class="number">2</span>][<span class="number">2</span>]);</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 向二维数组中存储元素</span></span><br><span class="line">        arr[<span class="number">0</span>][<span class="number">0</span>] = <span class="number">11</span>;</span><br><span class="line">        arr[<span class="number">0</span>][<span class="number">1</span>] = <span class="number">22</span>;</span><br><span class="line">        arr[<span class="number">0</span>][<span class="number">2</span>] = <span class="number">33</span>;</span><br><span class="line"></span><br><span class="line">        arr[<span class="number">1</span>][<span class="number">0</span>] = <span class="number">11</span>;</span><br><span class="line">        arr[<span class="number">1</span>][<span class="number">1</span>] = <span class="number">22</span>;</span><br><span class="line">        arr[<span class="number">1</span>][<span class="number">2</span>] = <span class="number">33</span>;</span><br><span class="line"></span><br><span class="line">        arr[<span class="number">2</span>][<span class="number">0</span>] = <span class="number">11</span>;</span><br><span class="line">        arr[<span class="number">2</span>][<span class="number">1</span>] = <span class="number">22</span>;</span><br><span class="line">        arr[<span class="number">2</span>][<span class="number">2</span>] = <span class="number">33</span>;</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 从二维数组中取出元素并打印</span></span><br><span class="line">        System.out.println(arr[<span class="number">0</span>][<span class="number">0</span>]);</span><br><span class="line">        System.out.println(arr[<span class="number">0</span>][<span class="number">1</span>]);</span><br><span class="line">        System.out.println(arr[<span class="number">0</span>][<span class="number">2</span>]);</span><br><span class="line">        System.out.println(arr[<span class="number">1</span>][<span class="number">0</span>]);</span><br><span class="line">        System.out.println(arr[<span class="number">1</span>][<span class="number">1</span>]);</span><br><span class="line">        System.out.println(arr[<span class="number">1</span>][<span class="number">2</span>]);</span><br><span class="line">        System.out.println(arr[<span class="number">2</span>][<span class="number">0</span>]);</span><br><span class="line">        System.out.println(arr[<span class="number">2</span>][<span class="number">1</span>]);</span><br><span class="line">        System.out.println(arr[<span class="number">2</span>][<span class="number">2</span>]);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h2 id="9-2-二维数组访问元素的细节问题"   >
          <a href="#9-2-二维数组访问元素的细节问题" class="heading-link"><i class="fas fa-link"></i></a><a href="#9-2-二维数组访问元素的细节问题" class="headerlink" title="9.2  二维数组访问元素的细节问题"></a>9.2  二维数组访问元素的细节问题</h2>
      <p>问题 :  二维数组中存储的是一维数组, 那能不能存入 [提前创建好的一维数组] 呢 ?</p>
<p>答 : 可以的</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.itheima.demo;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Demo2Array</span> &#123;</span><br><span class="line">    <span class="comment">/*</span></span><br><span class="line"><span class="comment">        问题: 二维数组中存储的是一维数组, 那能不能存入 [提前创建好的一维数组] 呢 ?</span></span><br><span class="line"><span class="comment">        答 : 可以的</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="type">int</span>[] arr1 = &#123;<span class="number">11</span>,<span class="number">22</span>,<span class="number">33</span>&#125;;</span><br><span class="line">        <span class="type">int</span>[] arr2 = &#123;<span class="number">44</span>,<span class="number">55</span>,<span class="number">66</span>&#125;;</span><br><span class="line">        <span class="type">int</span>[] arr3 = &#123;<span class="number">77</span>,<span class="number">88</span>,<span class="number">99</span>,<span class="number">100</span>&#125;;</span><br><span class="line"></span><br><span class="line">        <span class="type">int</span>[][] arr = <span class="keyword">new</span> <span class="title class_">int</span>[<span class="number">3</span>][<span class="number">3</span>];</span><br><span class="line"></span><br><span class="line">        arr[<span class="number">2</span>][<span class="number">3</span>] = <span class="number">100</span>;</span><br><span class="line"></span><br><span class="line">        arr[<span class="number">0</span>] = arr1;</span><br><span class="line">        arr[<span class="number">1</span>] = arr2;</span><br><span class="line">        arr[<span class="number">2</span>] = arr3;</span><br><span class="line"></span><br><span class="line">        System.out.println(arr[<span class="number">1</span>][<span class="number">2</span>]);</span><br><span class="line">        System.out.println(arr[<span class="number">2</span>][<span class="number">3</span>]);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h2 id="9-3-二维数组静态初始化"   >
          <a href="#9-3-二维数组静态初始化" class="heading-link"><i class="fas fa-link"></i></a><a href="#9-3-二维数组静态初始化" class="headerlink" title="9.3 二维数组静态初始化"></a>9.3 二维数组静态初始化</h2>
      <pre><code> 完整格式 : 数据类型[][] 变量名 = new 数据类型[][]&#123; &#123;元素1, 元素2...&#125; , &#123;元素1, 元素2...&#125; 
 
 简化格式 :  数据类型[][] 变量名 = &#123; &#123;元素1, 元素2...&#125; , &#123;元素1, 元素2...&#125; ...&#125;;
</code></pre>
<p>代码实现 : </p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.itheima.demo;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Demo3Array</span> &#123;</span><br><span class="line">    <span class="comment">/*</span></span><br><span class="line"><span class="comment">        完整格式：数据类型[][] 变量名 = new 数据类型[][]&#123; &#123;元素1, 元素2...&#125; , &#123;元素1, 元素2...&#125; ...&#125;;</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">        简化格式: 数据类型[][] 变量名 = &#123; &#123;元素1, 元素2...&#125; , &#123;元素1, 元素2...&#125; ...&#125;;</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="type">int</span>[] arr1 = &#123;<span class="number">11</span>,<span class="number">22</span>,<span class="number">33</span>&#125;;</span><br><span class="line">        <span class="type">int</span>[] arr2 = &#123;<span class="number">44</span>,<span class="number">55</span>,<span class="number">66</span>&#125;;</span><br><span class="line"></span><br><span class="line">        <span class="type">int</span>[][] arr = &#123;&#123;<span class="number">11</span>,<span class="number">22</span>,<span class="number">33</span>&#125;, &#123;<span class="number">44</span>,<span class="number">55</span>,<span class="number">66</span>&#125;&#125;;</span><br><span class="line">        System.out.println(arr[<span class="number">0</span>][<span class="number">2</span>]);</span><br><span class="line"></span><br><span class="line">        <span class="type">int</span>[][] array = &#123;arr1,arr2&#125;;</span><br><span class="line">        System.out.println(array[<span class="number">0</span>][<span class="number">2</span>]);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>




        <h1 id="10-递归"   >
          <a href="#10-递归" class="heading-link"><i class="fas fa-link"></i></a><a href="#10-递归" class="headerlink" title="10.递归"></a>10.递归</h1>
      <p>递归的介绍：</p>
<ul>
<li>以编程的角度来看，递归指的是方法定义中调用方法本身的现象</li>
<li>把一个复杂的问题层层转化为一个与原问题相似的规模较小的问题来求解</li>
<li>递归策略只需少量的程序就可描述出解题过程所需要的多次重复计算</li>
</ul>
<p>递归的基本使用：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//案例需求：	用递归求5的阶乘，并把结果在控制台输出</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">DiGuiDemo01</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="comment">//调用方法</span></span><br><span class="line">        <span class="type">int</span> <span class="variable">result</span> <span class="operator">=</span> jc(<span class="number">5</span>);</span><br><span class="line">        <span class="comment">//输出结果</span></span><br><span class="line">        System.out.println(<span class="string">&quot;5的阶乘是：&quot;</span> + result);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//定义一个方法，用于递归求阶乘，参数为一个int类型的变量</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="type">int</span> <span class="title function_">jc</span><span class="params">(<span class="type">int</span> n)</span> &#123;</span><br><span class="line">        <span class="comment">//在方法内部判断该变量的值是否是1</span></span><br><span class="line">        <span class="keyword">if</span>(n == <span class="number">1</span>) &#123;</span><br><span class="line">            <span class="comment">//是：返回1</span></span><br><span class="line">            <span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            <span class="comment">//不是：返回n*(n-1)!</span></span><br><span class="line">            <span class="keyword">return</span> n*jc(n-<span class="number">1</span>);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>

<p>内存图：</p>
<p><img src="https://s2.loli.net/2022/02/13/CHU18JMcmglVviQ.png" alt="递归内存图"></p>
<p>递归的注意事项：</p>
<ul>
<li>递归一定要有出口。否则内存溢出</li>
<li>递归虽然有出口，但是递归的次数也不宜过多。否则内存溢出</li>
</ul>
</div><footer class="post-footer"><div class="post-ending ending"><div class="ending__text">------ END ------</div></div><div class="post-copyright copyright"><div class="copyright-author"><span class="copyright-author__name">Author: </span><span class="copyright-author__value"><a href="http://bujiuzhi.gitee.io">不久</a></span></div><div class="copyright-link"><span class="copyright-link__name">Link: </span><span class="copyright-link__value"><a href="http://bujiuzhi.gitee.io/2022/02/12/1.Java%E5%9F%BA%E7%A1%80%E8%AF%AD%E6%B3%95/">http://bujiuzhi.gitee.io/2022/02/12/1.Java%E5%9F%BA%E7%A1%80%E8%AF%AD%E6%B3%95/</a></span></div><div class="copyright-notice"><span class="copyright-notice__name">Copyright: </span><span class="copyright-notice__value">All articles in this blog are licensed under <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.en" rel="external nofollow" target="_blank">BY-NC-SA</a> unless stating additionally</span></div></div><div class="post-tags"><span class="post-tags-item"><span class="post-tags-item__icon"><i class="fas fa-tag"></i></span><a class="post-tags-item__link" href="http://bujiuzhi.gitee.io/tags/%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/">学习笔记</a></span></div><div class="post-reward reward"><div class="reward-button">Buy me a coffee</div><div class="reward-qrcode"><span class="reward-qrcode-alipay"><img class="reward-qrcode-alipay__img" src="/images/alipay.jpg"><div class="reward-qrcode-alipay__text">Alipay</div></span><span class="reward-qrcode-wechat"><img class="reward-qrcode-wechat__img" src="/images/weixin.png"><div class="reward-qrcode-wechat__text">Wechat</div></span></div></div><nav class="post-paginator paginator"><div class="paginator-prev"><a class="paginator-prev__link" href="/2022/02/13/2.%E9%9D%A2%E5%AF%B9%E5%AF%B9%E8%B1%A1/"><span class="paginator-prev__icon"><i class="fas fa-angle-left"></i></span><span class="paginator-prev__text">2.面对对象</span></a></div><div class="paginator-next"><a class="paginator-next__link" href="/2022/02/05/1-Two-Sum/"><span class="paginator-prev__text">1.Two_Sum</span><span class="paginator-next__icon"><i class="fas fa-angle-right"></i></span></a></div></nav></footer></div></div><div class="comments" id="comments"><div id="valine-container"></div></div></div><div class="sidebar-wrap" id="sidebar-wrap"><aside class="sidebar" id="sidebar"><div class="sidebar-nav"><span class="sidebar-nav-toc current">Catalog</span><span class="sidebar-nav-ov">Overview</span></div><section class="sidebar-toc"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#1-%E6%A6%82%E8%BF%B0"><span class="toc-text">
          1. 概述</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#1-1-java%E8%AF%AD%E8%A8%80%E7%9A%84%E4%B8%89%E4%B8%AA%E7%89%88%E6%9C%AC"><span class="toc-text">
          1.1 java语言的三个版本</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#1-2-Java%E8%AF%AD%E8%A8%80%E8%B7%A8%E5%B9%B3%E5%8F%B0%E5%8E%9F%E7%90%86"><span class="toc-text">
          1.2 Java语言跨平台原理</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#1-3-JRE%E5%92%8CJDK"><span class="toc-text">
          1.3 JRE和JDK</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#1-3-1-JDK%E7%9A%84%E5%AE%89%E8%A3%85%E7%9B%AE%E5%BD%95%E4%BB%8B%E7%BB%8D"><span class="toc-text">
          1.3.1 JDK的安装目录介绍</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#1-4-HelloWorld%E6%A1%88%E4%BE%8B%E7%9A%84%E7%BC%96%E8%AF%91%E5%92%8C%E8%BF%90%E8%A1%8C"><span class="toc-text">
          1.4 HelloWorld案例的编译和运行</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#1-5-%E5%85%B3%E9%94%AE%E5%AD%97"><span class="toc-text">
          1.5 关键字</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#1-6-%E5%B8%B8%E9%87%8F"><span class="toc-text">
          1.6 常量</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#1-7-%E5%8F%98%E9%87%8F"><span class="toc-text">
          1.7 变量</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#1-7-1-%E5%8F%98%E9%87%8F%E7%9A%84%E5%AE%9A%E4%B9%89"><span class="toc-text">
          1.7.1 变量的定义</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#1-7-2-%E5%8F%98%E9%87%8F%E7%9A%84%E4%BF%AE%E6%94%B9"><span class="toc-text">
          1.7.2 变量的修改</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#1-7-3-%E5%8F%98%E9%87%8F%E7%9A%84%E6%B3%A8%E6%84%8F%E4%BA%8B%E9%A1%B9"><span class="toc-text">
          1.7.3 变量的注意事项</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#1-8-%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B"><span class="toc-text">
          1.8 数据类型</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#1-9-%E6%A0%87%E8%AF%86%E7%AC%A6"><span class="toc-text">
          1.9 标识符</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#1-9-1-%E7%BB%84%E6%88%90%E8%A7%84%E5%88%99%EF%BC%9A"><span class="toc-text">
          1.9.1 组成规则：</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#1-9-2-%E5%91%BD%E5%90%8D%E7%BA%A6%E5%AE%9A%EF%BC%9A"><span class="toc-text">
          1.9.2 命名约定：</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#1-9-2-1-%E5%B0%8F%E9%A9%BC%E5%B3%B0%E5%BC%8F%E5%91%BD%E5%90%8D%EF%BC%9A%E5%8F%98%E9%87%8F%E5%90%8D%E3%80%81%E6%96%B9%E6%B3%95%E5%90%8D"><span class="toc-text">
          1.9.2.1 小驼峰式命名：变量名、方法名</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#1-9-2-2-%E5%A4%A7%E9%A9%BC%E5%B3%B0%E5%BC%8F%E5%91%BD%E5%90%8D%EF%BC%9A%E7%B1%BB%E5%90%8D"><span class="toc-text">
          1.9.2.2 大驼峰式命名：类名</span></a></li></ol></li></ol></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#2-%E7%B1%BB%E5%9E%8B%E8%BD%AC%E6%8D%A2"><span class="toc-text">
          2.类型转换</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#2-1-%E9%9A%90%E5%BC%8F%E8%BD%AC%E6%8D%A2%EF%BC%88%E8%87%AA%E5%8A%A8%E7%B1%BB%E5%9E%8B%E8%BD%AC%E6%8D%A2%EF%BC%89"><span class="toc-text">
          2.1 隐式转换（自动类型转换）</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-2-%E5%BC%BA%E5%88%B6%E8%BD%AC%E6%8D%A2"><span class="toc-text">
          2.2 强制转换</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-3-%E7%B1%BB%E5%9E%8B%E8%BD%AC%E6%8D%A2%E6%A1%88%E4%BE%8B"><span class="toc-text">
          2.3 类型转换案例</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#3-%E8%BF%90%E7%AE%97%E7%AC%A6"><span class="toc-text">
          3.运算符</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#3-1-%E7%AE%97%E6%95%B0%E8%BF%90%E7%AE%97%E7%AC%A6"><span class="toc-text">
          3.1 算数运算符</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#3-1-1-%E5%AD%97%E7%AC%A6%E7%9A%84%E2%80%9C-%E2%80%9D%E6%93%8D%E4%BD%9C"><span class="toc-text">
          3.1.1 字符的“+”操作</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-1-2-%E5%AD%97%E7%AC%A6%E4%B8%B2%E7%9A%84%E2%80%9C-%E2%80%9D%E6%93%8D%E4%BD%9C"><span class="toc-text">
          3.1.2 字符串的“+”操作</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#3-2-%E8%87%AA%E5%A2%9E%E8%87%AA%E5%87%8F%E8%BF%90%E7%AE%97%E7%AC%A6"><span class="toc-text">
          3.2 自增自减运算符</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#3-3-%E8%B5%8B%E5%80%BC%E8%BF%90%E7%AE%97%E7%AC%A6"><span class="toc-text">
          3.3 赋值运算符</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#3-4-%E5%85%B3%E7%B3%BB%E8%BF%90%E7%AE%97%E7%AC%A6"><span class="toc-text">
          3.4 关系运算符</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#3-5-%E9%80%BB%E8%BE%91%E8%BF%90%E7%AE%97%E7%AC%A6"><span class="toc-text">
          3.5 逻辑运算符</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#3-6-%E7%9F%AD%E8%B7%AF%E9%80%BB%E8%BE%91%E8%BF%90%E7%AE%97%E7%AC%A6"><span class="toc-text">
          3.6 短路逻辑运算符</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#3-7-%E4%B8%89%E5%85%83%E8%BF%90%E7%AE%97%E7%AC%A6"><span class="toc-text">
          3.7 三元运算符</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#4-%E6%B5%81%E7%A8%8B%E6%8E%A7%E5%88%B6%E8%AF%AD%E5%8F%A5"><span class="toc-text">
          4. 流程控制语句</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#4-1-%E9%A1%BA%E5%BA%8F%E7%BB%93%E6%9E%84"><span class="toc-text">
          4.1 顺序结构</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#4-2-%E5%88%86%E6%94%AF%E7%BB%93%E6%9E%84"><span class="toc-text">
          4.2 分支结构</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#4-2-1-if%E8%AF%AD%E5%8F%A5%E6%A0%BC%E5%BC%8F1"><span class="toc-text">
          4.2.1  if语句格式1</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4-2-2-if%E8%AF%AD%E5%8F%A5%E6%A0%BC%E5%BC%8F2"><span class="toc-text">
          4.2.2 if语句格式2</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4-2-3-if%E8%AF%AD%E5%8F%A5%E6%A0%BC%E5%BC%8F3"><span class="toc-text">
          4.2.3 if语句格式3</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#4-2-%E5%BE%AA%E7%8E%AF%E7%BB%93%E6%9E%84"><span class="toc-text">
          4.2 循环结构</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#4-2-1-switch%E8%AF%AD%E5%8F%A5"><span class="toc-text">
          4.2.1 switch语句</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4-2-2-for%E5%BE%AA%E7%8E%AF"><span class="toc-text">
          4.2.2 for循环</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4-2-3-while%E5%BE%AA%E7%8E%AF"><span class="toc-text">
          4.2.3 while循环</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4-2-4-do%E2%80%A6%E2%80%A6while%E5%BE%AA%E7%8E%AF"><span class="toc-text">
          4.2.4 do……while循环</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4-2-5-%E4%B8%89%E7%A7%8D%E5%BE%AA%E7%8E%AF%E7%9A%84%E5%8C%BA%E5%88%AB"><span class="toc-text">
          4.2.5 三种循环的区别</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4-2-6-%E6%AD%BB%E5%BE%AA%E7%8E%AF"><span class="toc-text">
          4.2.6 死循环</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4-2-7-%E8%B7%B3%E8%BD%AC%E6%8E%A7%E5%88%B6%E8%AF%AD%E5%8F%A5"><span class="toc-text">
          4.2.7 跳转控制语句</span></a></li></ol></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#5-Random"><span class="toc-text">
          5.Random</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#5-1-Random%E4%BA%A7%E7%94%9F%E9%9A%8F%E6%9C%BA%E6%95%B0%EF%BC%88%E6%8E%8C%E6%8F%A1%EF%BC%89"><span class="toc-text">
          5.1 Random产生随机数（掌握）</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#6-%E6%95%B0%E7%BB%84"><span class="toc-text">
          6.数组</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#6-1-%E6%95%B0%E7%BB%84%E4%BB%8B%E7%BB%8D"><span class="toc-text">
          6.1 数组介绍</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#6-2-%E6%95%B0%E7%BB%84%E7%9A%84%E5%AE%9A%E4%B9%89%E6%A0%BC%E5%BC%8F"><span class="toc-text">
          6.2 数组的定义格式</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#6-3-%E5%8A%A8%E6%80%81%E5%88%9D%E5%A7%8B%E5%8C%96"><span class="toc-text">
          6.3 动态初始化</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#6-3-1-%E5%8A%A8%E6%80%81%E5%88%9D%E5%A7%8B%E5%8C%96%E6%A0%BC%E5%BC%8F"><span class="toc-text">
          6.3.1 动态初始化格式</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#6-3-2-%E7%A4%BA%E4%BE%8B%E4%BB%A3%E7%A0%81"><span class="toc-text">
          6.3.2 示例代码 :</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#6-4-%E6%95%B0%E7%BB%84%E7%9A%84%E9%9D%99%E6%80%81%E5%88%9D%E5%A7%8B%E5%8C%96"><span class="toc-text">
          6.4 数组的静态初始化</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#6-4-1-%E9%9D%99%E6%80%81%E5%88%9D%E5%A7%8B%E5%8C%96%E6%A0%BC%E5%BC%8F"><span class="toc-text">
          6.4.1 静态初始化格式</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#6-4-2-%E7%A4%BA%E4%BE%8B%E4%BB%A3%E7%A0%81"><span class="toc-text">
          6.4.2 示例代码</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#6-5-%E5%86%85%E5%AD%98%E5%88%86%E9%85%8D"><span class="toc-text">
          6.5 内存分配</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#7-%E6%96%B9%E6%B3%95"><span class="toc-text">
          7.方法</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#7-1-%E6%97%A0%E5%8F%82%E6%95%B0%E6%96%B9%E6%B3%95%E5%AE%9A%E4%B9%89%E5%92%8C%E8%B0%83%E7%94%A8"><span class="toc-text">
          7.1 无参数方法定义和调用</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#7-2-%E5%B8%A6%E5%8F%82%E6%95%B0%E6%96%B9%E6%B3%95%E5%AE%9A%E4%B9%89%E5%92%8C%E8%B0%83%E7%94%A8"><span class="toc-text">
          7.2 带参数方法定义和调用</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#7-3-%E5%B8%A6%E8%BF%94%E5%9B%9E%E5%80%BC%E6%96%B9%E6%B3%95%E5%AE%9A%E4%B9%89%E5%92%8C%E8%B0%83%E7%94%A8"><span class="toc-text">
          7.3 带返回值方法定义和调用</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#7-4-%E6%96%B9%E6%B3%95%E7%9A%84%E6%B3%A8%E6%84%8F%E4%BA%8B%E9%A1%B9"><span class="toc-text">
          7.4 方法的注意事项</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#7-5-%E6%96%B9%E6%B3%95%E9%87%8D%E8%BD%BD-%E5%90%8C%E6%96%B9%E6%B3%95%EF%BC%8C%E4%B8%8D%E5%90%8C%E5%8F%82"><span class="toc-text">
          7.5 方法重载(同方法，不同参)</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#7-6-%E6%96%B9%E6%B3%95%E7%9A%84%E5%8F%82%E6%95%B0%E4%BC%A0%E9%80%92"><span class="toc-text">
          7.6 方法的参数传递</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#7-6-1-%E4%BC%A0%E9%80%92%E5%9F%BA%E6%9C%AC%E7%B1%BB%E5%9E%8B"><span class="toc-text">
          7.6.1  传递基本类型</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#7-6-2-%E4%BC%A0%E9%80%92%E5%BC%95%E7%94%A8%E7%B1%BB%E5%9E%8B"><span class="toc-text">
          7.6.2 传递引用类型</span></a></li></ol></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#8-%E8%BF%9B%E5%88%B6"><span class="toc-text">
          8.进制</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#8-1-%E5%8D%81%E8%BF%9B%E5%88%B6%E5%88%B0%E4%BB%BB%E6%84%8F%E8%BF%9B%E5%88%B6%E7%9A%84%E8%BD%AC%E6%8D%A2"><span class="toc-text">
          8.1 十进制到任意进制的转换</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#8-2-%E5%8E%9F%E7%A0%81%E3%80%81%E8%A1%A5%E7%A0%81%E3%80%81%E5%8F%8D%E7%A0%81"><span class="toc-text">
          8.2 原码、补码、反码</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#8-3-%E4%BD%8D%E8%BF%90%E7%AE%97"><span class="toc-text">
          8.3 位运算</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#8-3-1-%E4%BD%8D%E8%BF%90%E7%AE%97%E7%AC%A6%E4%BB%8B%E7%BB%8D"><span class="toc-text">
          8.3.1 位运算符介绍 :</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#8-3-2-%E7%A4%BA%E4%BE%8B%E4%BB%A3%E7%A0%81"><span class="toc-text">
          8.3.2 示例代码</span></a></li></ol></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#9-%E4%BA%8C%E7%BB%B4%E6%95%B0%E7%BB%84%E6%A6%82%E8%BF%B0"><span class="toc-text">
          9.二维数组概述</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#9-1-%E4%BA%8C%E7%BB%B4%E6%95%B0%E7%BB%84%E5%8A%A8%E6%80%81%E5%88%9D%E5%A7%8B%E5%8C%96"><span class="toc-text">
          9.1 二维数组动态初始化</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#9-2-%E4%BA%8C%E7%BB%B4%E6%95%B0%E7%BB%84%E8%AE%BF%E9%97%AE%E5%85%83%E7%B4%A0%E7%9A%84%E7%BB%86%E8%8A%82%E9%97%AE%E9%A2%98"><span class="toc-text">
          9.2  二维数组访问元素的细节问题</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#9-3-%E4%BA%8C%E7%BB%B4%E6%95%B0%E7%BB%84%E9%9D%99%E6%80%81%E5%88%9D%E5%A7%8B%E5%8C%96"><span class="toc-text">
          9.3 二维数组静态初始化</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#10-%E9%80%92%E5%BD%92"><span class="toc-text">
          10.递归</span></a></li></ol></section><!-- ov = overview--><section class="sidebar-ov hide"><div class="sidebar-ov-author"><div class="sidebar-ov-author__avatar"><img class="sidebar-ov-author__avatar_img" src="/images/icons/cute.jpg" alt="avatar"></div><p class="sidebar-ov-author__text">星夜兼程</p></div><div class="sidebar-ov-social"><a class="sidebar-ov-social-item" href="https://gitee.com/bujiuzhi" target="_blank" rel="noopener" data-popover="social.gitee" data-popover-pos="up"><span class="sidebar-ov-social-item__icon">gitee</span></a><a class="sidebar-ov-social-item" href="https://github.com/bujiuzhi" target="_blank" rel="noopener" data-popover="Github" data-popover-pos="up"><span class="sidebar-ov-social-item__icon"><i class="fab fa-github"></i></span></a><a class="sidebar-ov-social-item" href="https://twitter.com/bujiuzhi" target="_blank" rel="noopener" data-popover="Twitter" data-popover-pos="up"><span class="sidebar-ov-social-item__icon"><i class="fab fa-twitter"></i></span></a><a class="sidebar-ov-social-item" href="https://space.bilibili.com/340646348" target="_blank" rel="noopener" data-popover="Youtube" data-popover-pos="up"><span class="sidebar-ov-social-item__icon">bilibili</span></a><a class="sidebar-ov-social-item" href="https://www.zhihu.com/people/bujiuzhi" target="_blank" rel="noopener" data-popover="Zhihu" data-popover-pos="up"><span class="sidebar-ov-social-item__icon">知</span></a></div><div class="sidebar-ov-state"><a class="sidebar-ov-state-item sidebar-ov-state-item--posts" href="/archives/"><div class="sidebar-ov-state-item__count">7</div><div class="sidebar-ov-state-item__name">Archives</div></a><a class="sidebar-ov-state-item sidebar-ov-state-item--categories" href="/categories/"><div class="sidebar-ov-state-item__count">3</div><div class="sidebar-ov-state-item__name">Categories</div></a><a class="sidebar-ov-state-item sidebar-ov-state-item--tags" href="/tags/"><div class="sidebar-ov-state-item__count">5</div><div class="sidebar-ov-state-item__name">Tags</div></a></div><div class="sidebar-ov-cc"><a href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.en" target="_blank" rel="noopener" data-popover="Creative Commons" data-popover-pos="up"><img src="/images/cc-by-nc-sa.svg"></a></div></section><div class="sidebar-reading"><div class="sidebar-reading-info"><span class="sidebar-reading-info__text">You have read </span><span class="sidebar-reading-info__num">0</span><span class="sidebar-reading-info__perc">%</span></div><div class="sidebar-reading-line"></div></div></aside></div><div class="clearfix"></div></div></main><footer class="footer" id="footer"><div class="footer-inner"><div><span>Copyright © 2023</span><span class="footer__icon"><i class="fas fa-heart"></i></span><span>不久</span></div><div><span>Powered by <a href="http://hexo.io/" title="Hexo" target="_blank" rel="noopener">Hexo</a></span><span> v6.3.0</span><span class="footer__devider">|</span><span>Theme - <a href="https://github.com/liuyib/hexo-theme-stun/" title="Stun" target="_blank" rel="noopener">Stun</a></span><span> v2.7.0</span></div></div></footer><div class="loading-bar" id="loading-bar"><div class="loading-bar__progress"></div></div><div class="back2top" id="back2top"><span class="back2top__icon"><i class="fas fa-rocket"></i></span></div></div><script src="https://cdn.jsdelivr.net/npm/jquery@v3.4.1/dist/jquery.min.js"></script><script src="https://cdn.jsdelivr.net/npm/velocity-animate@1.5.2/velocity.min.js"></script><script src="https://cdn.jsdelivr.net/npm/velocity-animate@1.5.2/velocity.ui.min.js"></script><script src="https://cdn.jsdelivr.net/npm/canvas-nest.js@1.0.1/dist/canvas-nest.min.js" color="0,0,0" opacity="0.6" count="99" zIndex="-1"></script><script src="https://cdn.jsdelivr.net/npm/leancloud-storage@latest/dist/av-min.js"></script><script src="https://cdn.jsdelivr.net/npm/valine@latest/dist/Valine.min.js"></script><script>function loadValine () {
  var GUEST_INFO = ['nick', 'mail', 'link'];
  var guest_info = 'nick,mail,link';

  guest_info = guest_info.split(',').filter(function(item) {
    return GUEST_INFO.indexOf(item) > -1;
  });
  new Valine({
    el: '#valine-container',
    appId: '5aO7FYnqH2acFvUnyGxifpjn-gzGzoHsz',
    appKey: 'eqj3y9AYWsueEDToYMjl0c5i',
    notify: true,
    verify: true,
    placeholder: 'Just go go',
    avatar: 'mp',
    meta: guest_info,
    pageSize: '10' || 10,
    visitor: false,
    recordIP: false,
    lang: '' || 'zh-cn',
    path: window.location.pathname
  });
}

if (false) {
  loadValine();
} else {
  window.addEventListener('DOMContentLoaded', loadValine, false);
}</script><script src="/js/utils.js?v=2.7.0"></script><script src="/js/stun-boot.js?v=2.7.0"></script><script src="/js/scroll.js?v=2.7.0"></script><script src="/js/header.js?v=2.7.0"></script><script src="/js/sidebar.js?v=2.7.0"></script></body></html>